Qondio
Front
Intel
IntelMart
Shares
My Qondio
Account
ndev2k > Intel > Getting my Head around Generics in C#

qondio.com/Bo56 PRINT EMAIL

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

Contributed by ndev2k on August 14, 2008, at 8:43 AM UTC.

PLEASE VISIT THE CONTRIBUTOR'S WEBSITE
Goodies For Free
Information about getting free gifts
goodies4free.net

Reactions

No reactions yet.

Rate This Intel

Please login or sign up to rate this intel.

Comments

Please login or sign up to add a comment.

Share

Copyright Notice

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.

Login Here with
Any Email Address
Any Password
No account? Sign up.

Intel Contributor
This intel was contributed by ndev2k

Qondio Archive
May, 2012
123456
78910111213
14151617181920
21222324252627
28293031


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

Sign Up
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.

About Qondio
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.

ABOUT
SUCCESS GUIDE
FEATURES
FAQ
ADVERTISE
CONTACT
USAGE POLICY
PRIVACY POLICY


TWITTER
FACEBOOK