Do generics get any performance advantages and pros and cons?

I am trying to generate some methods, as shown below, add crm and human resources . But I want to write Crm, humanResources, SupplChain, etc. (5-6 modules) how to write simple codes below (skeleton codes below :)) First I wrote without generics, and then I wrote using generics. The second use is different from the first. 1) Clear reading is also controlled and consumed
2) give me some performance advantages. There is no boxing and unboxing process.

CAN YOU CAN teach me the pros and cons of generics Comparing the first and second use?

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace App.GenericsBaseClass.Diff { // View : class Program { static void Main(string[] args) { new Crm().Add("Yapı Kredi"); Console.Read(); new HumanResources().Add("yusuf karatoprak"); Console.Read(); } } // Business public class Crm : Customer { public override void Add(object obj) { if (obj is String) { base.Add(obj); } else { throw new InvalidOperationException("Customer Name must be string Format..."); } } } public class HumanResources : Staff { public override void Add(object obj) { if (obj is string) { base.Add(obj); } else { throw new InvalidOperationException("Stuff Name must be string Format..."); } } } // Dal public class Staff { public virtual void Add(object obj) { new Db.Staff().Save((string)obj); } } public class Customer { public virtual void Add(object obj) { new Db.Customer().Save((string)obj); } } // Model public class Db { public class Customer { public void Save(string Name) { Console.WriteLine("Customer : {0} is saved",Name); } } public class Staff { public void Save(string Name) { Console.WriteLine("Staff: {0} is saved", Name); } } } } 
 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace App.GenericsInstadofBaseMethod { class Program { static void Main(string[] args) { new Crm<string>().Add("Yapı Kredi"); Console.Read(); new HumanResources<string>().Add("yusuf karatoprak"); Console.Read(); } } // Business public class Crm<T> : Customer<T> { public override void Add(T obj) { base.Add(obj); } } public class HumanResources<T> : Staff<T> { public override void Add(T obj) { base.Add(obj); } } // Dal public class Staff<T> { public virtual void Add(T obj) { new Db<T>.Staff().Save(obj); } } public class Customer<T> { public virtual void Add(T obj) { new Db<T>.Customer().Save(obj); } } // Model public class Db<T> { public class Customer { public void Save(T Name) { Console.WriteLine("Customer : {0} is saved", Name.ToString()); } } public class Staff { public void Save(T Name) { Console.WriteLine("Staff: {0} is saved", Name); } } } } 
+4
source share
5 answers

Aside from my post explaining some things about generics, your situation here is not like it really requires them at all. Instead, you should add the property to your class.

 public class Person { public string Name { get; set; } } public class Customer : Person { } public class Staff : Person { } public class HumanResources : Staff { } public class Db { public List<Customer> Customers { get; set; } public List<Staff> Staff { get; set; } } public static void Main(){ var db = new Db { Customers = new List<Customer> { }, Staff = new List<Staff> { } }; Db.Customers.Add(new Customer { Name = "Primary Customer" } ); Db.Staff.Add(new Staff { Name = "Employee Prime" } ); Db.Staff.Add(new HumanResources { Name = "Human Resource Manager" }); } 

Generics should be used to insist on the type that goes in, and not on the actual management of the data. The example you have does not use generics in its real purpose.

In this example, you create a base class, and then three subtypes, one of which inherits from the other subtype. Thus, you have Customer and Staff as the two main types that Db accepts (assuming it is a shorthand for the database).

Db can accept a Customer list and a Staff list, since HumanResources is a type of Staff , you can store them together. You can also add List<Person> People { get; set; } List<Person> People { get; set; } List<Person> People { get; set; } , and it will accept all types, since they inevitably inherit from each other.

As for forced strings, this is best done in your constructors.

 public class Person { public Person(string name) { this.Name = name; } } public string Name { get; private set; } } 

In this example, we set up the Name field so that it can be seen by everyone, but it only changes and is set during creation (not a good idea, but it shows a point).

So, now you would add a new Customer like this.

Db.Customers.Add( new Customer ( "Ciel" ) ); or new staff ... Db.Staff.Add( new Staff( "Darin" ) ); .

+2
source

From the fact that I understand little, most operations with Generics occur at the IL level, so there are not so many performance impacts, if any. All he does is force and validate objects of a certain type. The biggest is the understanding of contravariance and covariance. Here are some articles about this.

Articles

Using interface differences for shared collections

Frequently Asked Questions about Covariance and Contravariance (.NET 4.0 Update)

Pros

With generics, you get type safety both at runtime and in your development time. You can get intellisense about your objects in real time, as if they were hard-coded for a specific type. This is very useful when setting up both extension methods and ... and much more. In fact, starting with .NET 2.0, I don’t even find myself a more universal list for anything more, unless the type is object

  • Edit

As others have noted, this prevents the need for unpacking.

Vs

Due to the different nature of contravariance and covariance, confusion may arise as to what is and is not acceptable in labor time. For example, if you are considering the following layout.

 interface IBase { } class Alpha : IBase { } class Beta : Alpha { } IList<IBase> ListOfObjects { get; set; } 

This is confusing. Can you list Alpha and Beta objects in a list? When I tried, you couldn’t. But then it worked ...

 IList<Alpha> ListOfObjects { get; set; } 

He accepted Alpha and Beta objects because Beta inherits Alpha

Take a look at more information on contravariance and covariance here . It is very useful.

I have never noticed performance hits from generics. However, this does not mean that they do not exist. However, they are just simple objects, so I don’t see where the overhead will be. In the general case, the compiler should not even run code in which you try to add the wrong objects to the general list, and if so, somewhere you made a constructor error or use dynamics , or work on the issue of contravariance and covariance. Although I understand that some of them have changed since .NET 4.0.

+3
source

I do not think the performance difference is significant.

What you really get with generics is checking the compile time for types. This may prevent you from passing an object of the wrong type, for example (using the object parameter, runtime errors will occur if you expect a certain type).

+2
source

Generics are all about the type of security IMHO. You can write more reliable code because you get compile-time errors, not a trial version and horror.

Boxing is a bonus, but you should not choose generics to avoid boxing (which sounds like premature optimization).

+1
source

Here you can find a very interesting video on the advantages and disadvantages of using generics .

0
source

All Articles