Wednesday 1 February 2012

What are Generics in C#?



Ever check the difference in price of a brand name prescription drug against the price of it's generic counterpart?  What's the difference?  The generic prescription drug is usually considerably cheaper in price.  All of a sudden, I'm interested in Generics.

So what's with Generics in C#?  Some of the analogy is similiar.  Generics in C# (VS2005) are cheaper.  They are cheaper in debugging because they are type safe.  They are cheaper in performance because they run much faster than ArrayLists
.  They are cheaper in development time because there's no more boxing and unboxing.  They are cheaper stress wise because I no longer have to go through the nightmare of resizing an array in C# because it grew dynamically.  When I program arrays in VB.NET, I am pampered by the ReDim Preserve.

Generics did not come too soon.  In VB.NET, as in all previous versions of VB, at least as far back as VB3, I think (A long long time ago, in a galaxy far far away...), we have Collections.  Collections were neat in that they were scalable, or growable, but unless you boxed and unboxed
, you have not Intellisense and you have to use late-binding.  When the ArrayList came along in .NET, I thought, I'll never use an array again, because I hate Dimming and ReDimming arrays.  But at least an array could be strongly typed.

If I hated arrays in VB.NET, man, what a mess when I moved to C# and there was no ReDim Preserve!  Then I was faced with the use of an array that was not resizable without moving the contents to a temporary array while I resized the original array and then moved the contents back again.  What a mess!  But at least it was type-safe, which the ArrayList was not, again, unless I used boxing and unboxing.

Obviously, you can get around some of the aforementioned limitations by creating a Strongly Typed Collection every time you need a new collection, even if you have a tool like
NetRefactor, that can automatically create one for you.  It was something that I wish the system would have provided for me.  But now with the advent of Generics in C#, suddenly, this mess goes away.  I can add to a collection of strongly typed objects that performs must better than the ArrayList and is easier to use by far than an array, and I have Intellisense!,

I won't take the time to go into all of the new stuff in Generics.  Also, I am not so interested in creating a Generic Class, although you can read an execellent article on this that
Robert Chartier wrote.  The thing that intrigues me most is the new collections in the Generic Namespace.  The code shown below is a small example of using the new List collection.



public class Person
{
   private string _Name:
   private int _Age;
   public string Name
   {
      get{return _Name;}
      set{_Name = value;}
   }

   public int Age
   {
      get{return _Age;}
      set{_Age = value;}
   }
}

// now create a collection using the new Generic List
// Generic is a new namespace in System.Collections
private Generic.List<Person> employees = new Generic.List<Person>();

You will notice the new syntax for Generics in the line above that uses the "<>" to bracket the object.


// now add a couple of generic person objects to the generic collection
Person p1 = new Person();
p1.Name = "John";
p1.Age = 23;
employees.Add(p1);

Person p2 = new Person();
p2.Name = "James";
p2.Age = 34;
employees.Add(p2);

// now we have a type-safe collection of objects with the
// richness of Intellisense built in
// print out the contents of the collection
foreach (Person p in employees)
{
   Console.WriteLine(String.Format("(0) - {1}, p.Name, p.Age));
{

// the resulting output will be
John - 23
James - 34

And we did not have to do any boxing or casting up with all of their associated code, object creation, performance hit, and general convolution of the code.

No comments:

Post a Comment