A great example can be seen with typed collections.
It is good practice to create typed collections to return from a domain API
i.e. I have a PersonRepository with a method GetByLastName ... it is much
better practice for this to return a PersonCollection than say an ArrayList
(because the PersonCollection explicitly states that everything in the
collection is a person where as the arraylist could hold car objects as
well).
In 1.x you would have to write about 30 lines of code for every typed
collection that you wanted (if you google on typed collection I am sure you
can find a generator for such code). In 2.0 this is made much easier as you
can simply do something along the lines of PersonCollection :
Collection<Person> and avoid having to change things simply to change the
types that it is dealing with.
A quick answer is, many places you would have been untyped (or creating
specially typed versions of code in 1.x)
Generic methods can be useful anywhere that you were previously passing a
type in 1.x (especially if you were then returning an object of that type).
Imagine that you have a generic plugin loader ... You would probably use it
with something along the lines of
object o = MyPluginManager.CreatePluginFor(typeof(IMyInterfac e), "Name") or
IMyInterface foo = (IMyInterface)
MyPluginManager.CreatePluginFor(typeof(IMyInterfac e), "Name")
We can replace this in 2.0 with something like
IMyInterface foo = MyPluginManager.CreatePlugin<IMyInterface>("Name")
Notice how much our typing has changed.
Cheers,
Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
"VMI" <VM*@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
I understand the principle behind generics, but I'm not sure how I can
apply
them to a real-life situation.
Anybody have an example that shows how someone can benefit from a generic
class? The less complicated it is, the better :-)
Thanks.