real-life example of generics??

G

Guest

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.
 
G

Greg Young

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(IMyInterface), "Name") or
IMyInterface foo = (IMyInterface)
MyPluginManager.CreatePluginFor(typeof(IMyInterface), "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
 
M

Martin Carpella

Greg Young said:
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.

Why do you suggest using an explicit PersonCollection derived from
ICollection<Person> instead of using the ICollection<Person> directly?
Aesthetic reasons?

Best regards,
Martin
 
J

Joanna Carter [TeamB]

"Martin Carpella" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Why do you suggest using an explicit PersonCollection derived from
| ICollection<Person> instead of using the ICollection<Person> directly?
| Aesthetic reasons?

IMO, this is a hangover from .NET 1.1 and is totally unnecessary. Unless you
are adding extra functionality to List<T>, there is no case for deriving.

Joanna
 
A

antoan

Martin said:
Why do you suggest using an explicit PersonCollection derived from
ICollection<Person> instead of using the ICollection<Person> directly?
Aesthetic reasons?

Best regards,
Martin


I'm guessing, to create a more specialized collection (PersonCollection
) with custom functionality. Thats all.

A
 
G

Greg Young

I missed some messages here ..

The reason for using a typed collection is the possibility of needing to add
functionality in the future.

Consider ...

public PersonCollection GetPeople() {}

vs

public Collection<Person> GetPeople() { }

Although at this moment PersonCollection may be implemented as

PersonCollection : Collection<Person>{}

It may in the future have methods added on to it. Since I have a public
contract being exposed here that future change could break an unknown number
of client locations where as returning a typed PersonCollection insures that
I will not break any client code if I decide I need to make the change in
the future.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top