|
   |
|
|
Getting my Head around Generics in C#
Generics can be hard for people to get their head around by virtue of the fact that they are type safe, yet they have no type (in theory). The best way to get your head around Generics is to look at their most common usage. Collections. Now, I am not sure if you know, but before .NET 2.0, if you wanted to create a Collection class (a class that contains 0 or more of something) then you would add them to something like an ArrayList or HashTable etc. The problem with this is that these collections store everything as Object. This means then when you retrieve something from the collection, you need to cast it back to its original type, which can make code untidy, as well as be a pain in the ass because it would take 2-3 lines of extra code to get back to your original [typed] value. The same problem occurs with adding objects to the collection. Since they take object, you can add ANY object to it. For example, if you have a an ArrayList of Customer objects called Customers, you could easily do Customers.Add(new FunkyHat()). Now, while I am sure the customers would love a look at the hat, its not supposed to be there. Now, in 1.1 the only way to get around this was to create custom Collection classes, which would wrap the Collection class (which saves as Object) and provides type safety. So you would have a CustomerCollection class, which internally had a ArrayList, but you were never aware of it. You could then control what came in and out of the collection by specifying the parameter and return types. So, what do Generics do? Well, they take ALL that away! What you do with generics is specify the type they are to enforce on instantiation. E.g List<string> internally is an ArrayList, but will actually only ever accept/return a string. This saves a LOT of coding overhead, since we no longer need to create Custom Collection classes for simple collections (not to imply you NEVER need to of course!)
|
Contributor's Note
I am studying C# programming and some things are difficult to get my head round. I will post how I understand them when I do. Hopefully can help others
|
|
|
 |
|
PLEASE VISIT THE CONTRIBUTOR'S WEBSITE
No reactions yet.
Please login or sign up to rate this intel.
Please login or sign up to add a comment.
The copyright for this content entitled "Getting my Head around Generics in C#" has been specified by the contributor as:
All Rights Reserved
This content may not be copied, distributed or adapted by anyone under any circumstances.
|
 |
|
This intel was contributed by ndev2k
|
May, 2012
2008
January, February, March, April, May, June, July, August, September, October, November, December
2009
January, February, March, April, May, June, July, August, September, October, November, December
2010
January, February, March, April, May, June, July, August, September, October, November, December
2011
January, February, March, April, May, June, July, August, September, October, November, December
2012
January, February, March, April, May
|
|
Not a member yet?
Qondio is a powerful network for making it online. If you have a website to
promote, we can help.
Sign up and get in on the action.
|
|
Welcome to Qondio! Discover the awesome power this network can deliver by going to our About page. Or you could skip straight to the Sign Up form.
|
|