473,545 Members | 2,543 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple question about complicated questions about Generics.

Why do people spend so much time writing complex generic types?

for fun?
to learn?
for use?

I think of generics like I do about operator overloading.
Great to have as a language-feature, as it defines the language more
completely. Great to use.

But code one?

While I love to use generic classes; I seldom need to code my own.
How often do you guys code your own generic classes? why? what for?

Seriously Asking
- Michael S



Dec 29 '05 #1
12 2721
I think the answer to you question depends on how much knowledge about
Generics a person has.

A person with limited Generics knowledge will probably not use Generics that
often but a person that has digged deep into the whys and how's about
Generics will probably be able to leverage all that knowledge and apply
Generics into scenarios that most people didn't even know was possible.

There's a book called "Profession al .Net 2.0 Generics" by Wrox, read it from
cover to cover and take the time to play around with the examples to fully
understand what's going on and realize the potential on Generics. After
that, maybe you will start using Generics far more than what you are doing
now!!

Have a good day.
Dec 29 '05 #2
Rene <no****@nospam. com> wrote:
I think the answer to you question depends on how much knowledge about
Generics a person has.

A person with limited Generics knowledge will probably not use Generics that
often but a person that has digged deep into the whys and how's about
Generics will probably be able to leverage all that knowledge and apply
Generics into scenarios that most people didn't even know was possible.

There's a book called "Profession al .Net 2.0 Generics" by Wrox, read it from
cover to cover and take the time to play around with the examples to fully
understand what's going on and realize the potential on Generics. After
that, maybe you will start using Generics far more than what you are doing
now!!


That may not be a good thing, of course. There are lots of things which
*can* be done in generics but could also be done more simply without
generics.

I'm not suggesting that generics are a bad thing by any means - just
that they can be overused simply because a developer knows them and
wants to play with fun new technology.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 29 '05 #3
On Thu, 29 Dec 2005 03:22:15 +0100, "Michael S" <no@mail.com> wrote:
Why do people spend so much time writing complex generic types?
Um, what people? Who or what are you talking about?
How often do you guys code your own generic classes? why? what for?


For my own custom collection classes, and utility methods that operate
on generic collections. Also, for events that take a single argument
of an arbitrary type. That's it, basically...
--
http://www.kynosarges.de
Dec 29 '05 #4
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de news:
MP************* ***********@msn ews.microsoft.c om...

| That may not be a good thing, of course. There are lots of things which
| *can* be done in generics but could also be done more simply without
| generics.
|
| I'm not suggesting that generics are a bad thing by any means - just
| that they can be overused simply because a developer knows them and
| wants to play with fun new technology.

This is exactly the scenario that we found ourselves in. We had a definite
requirement for a few generic classes, but then we found that we tended to
automatically try to use generic classes for everything.

I think generics should be used in the same way as inheritance :

Instead of designing a super-class and then trying to derive from it, you
should always design separate classes and only when you start to recognise
commonality, extract the super-class.

So with generics, you should start by designing separate typed classes and
then, if their behaviour is truly identical, replace them with a generic
type.

I have written an article on using generics in Design Patterns for the UK
Developers Group magazine which demonstrates this principle and which is
available at http://www.prototypical.co.uk/pdf/visitor.pdf

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 29 '05 #5
Joanna,

Sorry to say. Bad advise both on parametrized classes as well as
on Inheritance.

On Thu, 29 Dec 2005 10:40:28 -0000, "Joanna Carter [TeamB]"
<jo****@not.for .spam> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de news:
MP************ ************@ms news.microsoft. com...

| That may not be a good thing, of course. There are lots of things which
| *can* be done in generics but could also be done more simply without
| generics.
|
| I'm not suggesting that generics are a bad thing by any means - just
| that they can be overused simply because a developer knows them and
| wants to play with fun new technology.

This is exactly the scenario that we found ourselves in. We had a definite
requirement for a few generic classes, but then we found that we tended to
automaticall y try to use generic classes for everything.

I think generics should be used in the same way as inheritance :

Instead of designing a super-class and then trying to derive from it, you
should always design separate classes and only when you start to recognise
commonality, extract the super-class.
Surely code bloat and absurd design will result. Bottom up design is
famous for bad decisions. Let alone that "commonalit y" isnt even
exactly defined. Use of the ADT approach is widely considered
superior, and indeed that starts off with abstract AND related
"superclass es" or interfaces. Thats far better then relate all kind of
concrete classes and then refactor into some kind of hierarchie.

There will never be a good framework without good abstract concepts
definied into abstract classes and/or interfaces.

So with generics, you should start by designing separate typed classes and
then, if their behaviour is truly identical, replace them with a generic
type.

Absurd. If you understand the parametrization and its forces.
I have written an article on using generics in Design Patterns for the UK
Developers Group magazine which demonstrates this principle and which is
available at http://www.prototypical.co.uk/pdf/visitor.pdf

very good generics beginners article Joanna. It has imho not a lot to
do with the proffesional way of using parametrized classes though. The
visitor pattern and all its parent double dispatchers is already
something very obscure and not often used, let alone some absurd
trivial ValueType class.

Proffesional use of generics will mostly be valuable by people wrting
framework code to be used by others. They will know and understand
the use as they will do with inheritance and delegation because they
have to do design. If we are talking people who just code away custom
apps with 1 user or buyer, then they will not benifit from no generic
code( that is coding towards abstractions implemented in c# with
interfaces, abstract classes, inheritance and generics). Most people
seem to use c# as vb, but some are using it like c/c++ and they will
fully appreciate the generics..

For really strong examples you probably should look into delegates
and other observer implementations and in abstract factories instead
of the OO defeating visitorpattern.
Joanna


Rick

Dec 29 '05 #6
"Rick Elbers" <ri*********@ch ello.nl> a écrit dans le message de news:
6a************* *************** ****@4ax.com...

| Sorry to say. Bad advise both on parametrized classes as well as
| on Inheritance.

In your opinion maybe.

| Surely code bloat and absurd design will result.

Well, it never has over the past ten or so years for me.

| Bottom up design is
| famous for bad decisions. Let alone that "commonalit y" isnt even
| exactly defined. Use of the ADT approach is widely considered
| superior, and indeed that starts off with abstract AND related
| "superclass es" or interfaces. Thats far better then relate all kind of
| concrete classes and then refactor into some kind of hierarchie.

Not in my experience.

| There will never be a good framework without good abstract concepts
| definied into abstract classes and/or interfaces.

Correct, but that doesn't mean that you have to design the abstract classes
before the concrete ones.

| >So with generics, you should start by designing separate typed classes
and
| >then, if their behaviour is truly identical, replace them with a generic
| >type.
|
| Absurd. If you understand the parametrization and its forces.

Well, I have had no problems.

| very good generics beginners article Joanna. It has imho not a lot to
| do with the proffesional way of using parametrized classes though. The
| visitor pattern and all its parent double dispatchers is already
| something very obscure and not often used, let alone some absurd
| trivial ValueType class.

You seem to think a lot of things are absurd :-) The ValueType class is a
real world example of a metadata-rich framework for object persistence and
UI presentation of objects.

| Proffesional use of generics will mostly be valuable by people wrting
| framework code to be used by others. They will know and understand
| the use as they will do with inheritance and delegation because they
| have to do design.

Frameworks form a large part of my work, which is why I have found so much
use for generics

| Most people
| seem to use c# as vb, but some are using it like c/c++ and they will
| fully appreciate the generics..

Coming from a C/C++/Delphi background, I can't really say what a VB
programmer thinks of C#. I find it a very nice Cesque version of Delphi;
capable of most things from drag and drop RAD development right through to
complex fully OO model driven frameworks and utilities.

| For really strong examples you probably should look into delegates
| and other observer implementations and in abstract factories instead
| of the OO defeating visitorpattern.

The article was the first in a series of articles on design patterns in C#
using generics; other articles cover things like the Observer pattern and
other patterns.

I find your comment that the Visitor pattern is OO defeating quite odd, as
it is a pattern that allows you to maintain the integrity of a hierarchy
whilst adding common behaviour throughout the hierarchy in a type specific
manner.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 29 '05 #7
I use the generics when I want to factor out duplicated code, and the
duplicated bits have some variation on type.

Many times I wish that C# generics were more powerful. For example, I
would like to write code like this, which takes a boolean function and
returns a new function returning the opposite value of the original:

delegate bool Predicate<T>(T arg);

Predicate<T> Not<T>(Predicat e<T> f)
{
return new Predicate<T>(de legate(T t) { return !f(t); } )
}

Dec 30 '05 #8

"Rick Elbers" <ri*********@ch ello.nl> wrote in message
news:6a******** *************** *********@4ax.c om...
Joanna,

Sorry to say. Bad advise both on parametrized classes as well as
on Inheritance.

On Thu, 29 Dec 2005 10:40:28 -0000, "Joanna Carter [TeamB]"
<jo****@not.for .spam> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> a écrit dans le message de news:
MP*********** *************@m snews.microsoft .com...

| That may not be a good thing, of course. There are lots of things which
| *can* be done in generics but could also be done more simply without
| generics.
A truism! A lot of words but no content...
| I'm not suggesting that generics are a bad thing by any means - just
| that they can be overused simply because a developer knows them and
| wants to play with fun new technology.

This is exactly the scenario that we found ourselves in. We had a definite
requirement for a few generic classes, but then we found that we tended to
automatical ly try to use generic classes for everything.
This is exactly whats going to happen if you let a couple of geeks decide
architecture.

I think generics should be used in the same way as inheritance :

Instead of designing a super-class and then trying to derive from it, you
should always design separate classes and only when you start to recognise
commonality , extract the super-class.
Oh, you been at school? Did you score well? Why should you 'always' do
stuff?
Surely code bloat and absurd design will result. Bottom up design is
famous for bad decisions. Let alone that "commonalit y" isnt even
exactly defined. Use of the ADT approach is widely considered
superior, and indeed that starts off with abstract AND related
"superclass es" or interfaces. Thats far better then relate all kind of
concrete classes and then refactor into some kind of hierarchie.

There will never be a good framework without good abstract concepts
definied into abstract classes and/or interfaces.

So with generics, you should start by designing separate typed classes and
then, if their behaviour is truly identical, replace them with a generic
type.

Should we? I think not. I would like for you to combine 'time/money' and
'experience' into this. Did you copy this from a textbook? What was their
point? Do you have any ideas of your own?


Absurd. If you understand the parametrization and its forces.
I have written an article on using generics in Design Patterns for the UK
Developers Group magazine which demonstrates this principle and which is
available at http://www.prototypical.co.uk/pdf/visitor.pdf


very good generics beginners article Joanna. It has imho not a lot to
do with the proffesional way of using parametrized classes though. The
visitor pattern and all its parent double dispatchers is already
something very obscure and not often used, let alone some absurd
trivial ValueType class.


Beginners? Are you that clueless? Joanna Carter more or less set the stage
for MVC and are a major spirit in anything revolving OO. Tjeesus!

Proffesional use of generics will mostly be valuable by people wrting
framework code to be used by others. They will know and understand
the use as they will do with inheritance and delegation because they
have to do design. If we are talking people who just code away custom
apps with 1 user or buyer, then they will not benifit from no generic
code( that is coding towards abstractions implemented in c# with
interfaces, abstract classes, inheritance and generics). Most people
seem to use c# as vb, but some are using it like c/c++ and they will
fully appreciate the generics..

For really strong examples you probably should look into delegates
and other observer implementations and in abstract factories instead
of the OO defeating visitorpattern.
Joanna


Rick


While I think Joanna is somewhat way too 'model', like dear mr. Skeet, for
my taste, at least they known what their doing...

Happy<T>
- Michael S
Dec 30 '05 #9
"kevin cline" <ke*********@gm ail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
I use the generics when I want to factor out duplicated code, and the
duplicated bits have some variation on type.

Many times I wish that C# generics were more powerful. For example, I
would like to write code like this, which takes a boolean function and
returns a new function returning the opposite value of the original:

delegate bool Predicate<T>(T arg);

Predicate<T> Not<T>(Predicat e<T> f)
{
return new Predicate<T>(de legate(T t) { return !f(t); } )
}


Will C#3 solve this? Make it easier?
Jon, Nicholas? Any thoughts.....
Dec 30 '05 #10

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

Similar topics

31
14296
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? Basically: "Press any key to continue..." I beleive that I am looking for is something along the lines of a....
1
2136
by: Keith R | last post by:
Currently, generic types are not CLS compliant. This puts library authors in a quandry who are faced with three bad alternatives: 1. CLS Compliance but no generics, 2. Using generic types but losing CLS Compliance, or 3. Supporting both, bloating the code and muddying the waters with dual versions of generic classes. This latter is seen in...
6
2830
by: Daniel O'Connell | last post by:
Two questions here: 1. Is there any particular reason why when using stackalloc, the code byte *buffer = stackalloc byte; works, but code like byte *buffer; buffer = stackalloc byte; is considered incorrect syntax? Was this an oversight, a stylistic design, or is there a technical reason it won't work? 2. I was reading an older(circa...
3
1456
by: C# Learner | last post by:
1) When's C# 2.0 "coming out"? 2) Will all the .NET collection classes (e.g. ArrayList) be rewritten to use generics instead of object references? 3) This one's off-topic -- will Borland's C# Builder be up and ready for 2.0 when it's "out"?
2
1676
by: D H | last post by:
Hi, Hi, I was looking for your opinion on VB.NET - its long-term prospects, etc. Sorry this is vague, but it's not meant to be a troll. It's a pro-VB.NET post actually. I haven't used VB or VB.NET really, but I have used Realbasic (a vb clone), Java, C#, C, and Python. I work in education, and I'd like something that is easy enough for...
22
2532
by: tricard | last post by:
Good evening, I was thinking about making a very simple DOS based (console) menu for a project that i am working on, but I have no idea where to start for something like this. Does anyone have any resources they could point me to or shed some light on what it is that you do exactly. Like I said, I don't want a real complicated menu, just a...
1
1881
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList = new List<IPerson>(); ... // get the persons return personList;
4
4372
by: Shawnk | last post by:
This post is intended to verify that true value semantics DO NOT EXIST for the Enum class (relative to boolean operations). If this is true then (thus and therefore) you can not design state machines in C# where the machine can use the enumerated 'state' to execute (thus the term 'machine' as in 'state machine') the machine operations as...
17
5790
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
7464
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7413
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.