The terms generic and non-generic with respect to collections

Z

Zach

Having learned hat:
( 1. ) Generic collections / type-safe / hold value types: Array (ex Net
0.0.), List and Dictionary (ex Net v2.0.).
( 2. ) Non-generic collections / not type-safe / hold object types:
ArrayList and Hashtable (ex Net v1.0).

Assuming ( 1. ) and ( 2. ) are correct:
I have a problem with the terms. Generic from genus is more general than
species. Given that ( 1. ) applies to value types, cannot contain all kinds
of types, why the use of "generic", that seems contradictory to me.

Please explain.
 
J

Jason Keats

Zach said:
Having learned hat:
( 1. ) Generic collections / type-safe / hold value types: Array (ex Net
0.0.), List and Dictionary (ex Net v2.0.).
( 2. ) Non-generic collections / not type-safe / hold object types:
ArrayList and Hashtable (ex Net v1.0).

Assuming ( 1. ) and ( 2. ) are correct:
I have a problem with the terms. Generic from genus is more general than
species. Given that ( 1. ) applies to value types, cannot contain all
kinds of types, why the use of "generic", that seems contradictory to me.

Please explain.

I think you need to do a little more research. Maybe start here:
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx

You will find that writing your own generic method or collection means
that you don't have to write more specific methods or collections for
each data type (string, integer, etc.).

In that sense, generics are more generic.

I believe the compiler generates the specialised code required for each
data type - from your generic description.
 
Z

Zach

Jason Keats said:
I think you need to do a little more research. Maybe start here:
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx

You will find that writing your own generic method or collection means
that you don't have to write more specific methods or collections for each
data type (string, integer, etc.).

In that sense, generics are more generic.

I believe the compiler generates the specialised code required for each
data type - from your generic description.

Your reference answers my query in that it states: << Generics allow you to
define ...structures, without committing to actual data types. >> So the
generic aspect comes into play with resprect to the applicability, rather
that the essence of the collections in quesion. (PS I don't have a problem
working with generics, I was only puzzled by the terminilogy as I was
editing the C# manual I have written.)
 
M

Mike Schilling

Peter Duniho said:
For each use of a generic type with a specific type parameter, the
compiler will generate a specialized type. This will include strongly
typed static members. And for value types as the type parameter, a new
copy of all the code is required for each use. But for reference types,
since type checking can be done at compile time (with explicit casts as
needed by the programmer to satisfy the compile-time rules), all of the
reference-type-based generics can share the same code.

In that respect, while .NET generics is similar to C++ templates, it can
be and often is significantly more efficient in terms of code generation
than C++ templates.

Also note that .NET generics are quire different from Java generics, which
do not generate specific run-time types, but instead "erase" type parameters
at run-time. (I do *not* mean to start a flame war about which is superior,
just to point out that someone who programs in both environments needs to
understand the differences.)
 
Z

Zach

Peter Duniho said:
Having learned hat:
( 1. ) Generic collections / type-safe / hold value types: Array (ex Net
0.0.), List and Dictionary (ex Net v2.0.).
( 2. ) Non-generic collections / not type-safe / hold object types:
ArrayList and Hashtable (ex Net v1.0).

Assuming ( 1. ) and ( 2. ) are correct:

They are not. Generic types (not just collection types) may (and often
do) use reference types as the type parameters for the type. As well,
non-generic collection types may hold value types that have been boxed;
technically, they are holding a reference type, but from a semantic point
of view, the only valid types in those collection instances are the value
type for the collection.

(You seem to be using the term "object type" to mean what is correctly
called a "reference type". Especially if you are writing a manual for the
language, it is very important that you use the correct terminology. All
types in .NET are "object types", in that they all inherit System.Object)
I have a problem with the terms. Generic from genus is more general than
species. Given that ( 1. ) applies to value types, cannot contain all
kinds of types, why the use of "generic", that seems contradictory to me.

Generic collections can contain members of _any_ type. Further, as Jason
has explained, the type itself is not specific to any particular type, and
thus is general-purpose, or "generic". Non-generic collection types may
use System.Object as their member type (e.g. ArrayList), or they may use
some more-derived type (e.g. a custom type with strongly typed input and
output members). But they are always special-purpose, in that they can't
be strongly typed with anything other than a single specific type.

Finally, from Jason's reply, a minor clarification:

[...]
I believe the compiler generates the specialised code required
for each data type - from your generic description.

For each use of a generic type with a specific type parameter, the
compiler will generate a specialized type. This will include strongly
typed static members. And for value types as the type parameter, a new
copy of all the code is required for each use. But for reference types,
since type checking can be done at compile time (with explicit casts as
needed by the programmer to satisfy the compile-time rules), all of the
reference-type-based generics can share the same code.

In that respect, while .NET generics is similar to C++ templates, it can
be and often is significantly more efficient in terms of code generation
than C++ templates.

Pete

Pete, thank you very much for your much appreciated response. I am having
some semantic problems: apologies about that. So please allow me to
summarize what you have written, so I might better understand. I hope I am
not disappointing you in that the manual I mentioned is for my own use. I
have written a number of published applications, however in a limited domain
(mostly finance) requiring only a subset of C#-kowledge, so from time to
time I work through my manual again, and attempt to top up my knowledge,
prompted by the questions that I see being asked.

Assumption: you are using the terms type parameter and member type
synonymously (in the case of ArrayList, “string†could be the type
parameter), in your writing both mean “containâ€.

What is a specialized type? Is it a custom or reference type?
What is a specific type? Is it a non-custom type?

Applicable to both:
---------------------
Both generic and non-generic types inherit from System.Object.

Applicable to each:
---------------------
Generic types: e.g., List and Dictionary
Generic means: can contain members of any type, in other words, they are
general purpose. They often contain reference types. If they contain
specific types, the compiler will generate specialized types. If they
contain value types, the compiler will copy the code for each use.

Non-generic types: e.g., ArrayList and Hashtable
Non-generic means special purpose in that they must contain a single
specific type, a boxed value type, System.Object, or a more-derived type,
e.g. custom type.
 
A

Arne Vajhøj

Having learned hat:
( 1. ) Generic collections / type-safe / hold value types: Array (ex Net
0.0.), List and Dictionary (ex Net v2.0.).
( 2. ) Non-generic collections / not type-safe / hold object types:
ArrayList and Hashtable (ex Net v1.0).

Assuming ( 1. ) and ( 2. ) are correct:
I have a problem with the terms. Generic from genus is more general than
species. Given that ( 1. ) applies to value types, cannot contain all
kinds of types, why the use of "generic", that seems contradictory to me.

As already noted by others then generic colelctions can
contain both value and reference types.

But no matter what the the term "generic" in programming
languages has a very specific meaning.

See http://en.wikipedia.org/wiki/Generic_programming for
some details.

System.Collections.Generic matches this definition.

Arne
 
Z

Zach

Peter Duniho said:
Manuals written for one's own use have a surprising way of making their
way into other people's libraries nonetheless. :) . . . snipped

It has happened :(

Pete, thank you very much for your impressive response. Apologies for my
original query post which was rather shambollocks, thank you for not saying
so.
 

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