473,394 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

real-life example of generics??

VMI
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.
Jun 15 '06 #1
5 5435
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.

Jun 15 '06 #2
"Greg Young" <dr*******************@hotmail.com> writes:
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
Jun 23 '06 #3
"Martin Carpella" <ma*************@gmx.net> a écrit dans le message de news:
87************@msgid.carpella.net...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 23 '06 #4
Martin Carpella wrote:
"Greg Young" <dr*******************@hotmail.com> writes:
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

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

A
Jun 27 '06 #5
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
"antoan" <an****@antoan.plus.com> wrote in message
news:44***********************@ptn-nntp-reader01.plus.net...
Martin Carpella wrote:
"Greg Young" <dr*******************@hotmail.com> writes:
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

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

A

Jul 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
4
by: Aaron W. West | last post by:
Timings... sometimes there are almost too many ways to do the same thing. The only significant findings I see from all the below timings is: 1) Integer math is generally fastest, naturally....
4
by: Allan Adler | last post by:
I'm trying to reinstall RedHat 7.1 Linux on a PC that was disabled when I tried to upgrade from RH7.1 to RH9. This is presenting lots of unexpected difficulties. Apart from wanting to keep the old...
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
5
by: Henry Wu | last post by:
Hi, now that in .NET everything is on millimeter, I was wondering how can one convert Pixel to Millimeter and any user's screen/monitor. I saw the following code on how to convert pixel to...
0
by: support | last post by:
Veteran Real Estate Investor Shares some of his best Insider Secrets for successful investments! www.RealEstateBeginners.ws Have you ever wondered about investing in real estate? Maybe one...
34
by: Guch Wu | last post by:
Boost has many terrific libraries. But I want to know whether they are ready for using in real projects. Which of them are mature enough, or just only in progress?
16
by: DirtyHarry | last post by:
Good day everyone. This sounds like a stupid question, but I became just curious yesterday, and I looked up several textbooks. However, no textbooks on computer language (that I have ) mentioned...
2
by: Blubaugh, David A. | last post by:
To All, I have done some additional research into the possibility of utilizing Python for hard real time development. I have seen on various websites where this has been discussed before on...
3
by: Kurt Mueller | last post by:
David, Am 07.10.2008 um 01:25 schrieb Blubaugh, David A.: As others mentioned before, python is not the right tool for "HARD REAL TIME". But: Maybe you can isolate the part of your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.