Generics

N

nick_nw

Hi,

What significant advantages do generics give me over passing objects
around as 'object' and casting when needed?

I was asked this recently and started off by saying, "well of course
generics are better because, er.... hang on lets ask the ng!"

So hence the q.

Thanks,

Nick
 
J

Joanna Carter [TeamB]

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

| What significant advantages do generics give me over passing objects
| around as 'object' and casting when needed?
|
| I was asked this recently and started off by saying, "well of course
| generics are better because, er.... hang on lets ask the ng!"

Because generic types are typesafe at compile-time and cannot be
accidentally miscoded to accept the wrong type.

{
List<int> list = new List<int>();

list.Add(1234M) // will not compile

list.Add(1234) // will compile OK
}

Joanna
 
C

Chris Fulstow

Hi Nick,

Generics let you create collections that are type-safe, so there's no
risk of adding an object of the wrong type to your collection.

This alse means that there's no need to cast objects that you retieve
from your collections, which results in cleaner code.

HTH,
Chris
 
F

Fred Mellender

1. It makes the code easier to read and write. Type assumptions are made
explicit.
2. It allows some type-checking to be done at compile time instead of
runtime, so that coding errors are easier to catch and fix.
 
M

Mike

Correct. But you can create type-safe collections by deriving a new
class from System.Collections.CollectionBase,
System.Collections.DictionaryBase which would only allow a single type
to be added. However, in that case you need a new collection class for
each type you want to store in a collection.

What generics add is the ability to create a single class for holding
all collections, and still doing it in a type safe manner. Actually,
the framework comes with these classes already. See
System.Collections.Generic
http://msdn2.microsoft.com/en-us/library/system.collections.generic(VS.80).aspx

Michael Lang
XQuiSoft LLC
http://www.xquisoft.com/
 
J

Jon Skeet [C# MVP]

nick_nw said:
What significant advantages do generics give me over passing objects
around as 'object' and casting when needed?

I was asked this recently and started off by saying, "well of course
generics are better because, er.... hang on lets ask the ng!"

One thing which has been missed so far is the better handling of value
types for things like collections. Using generics, T[] really *is* a
byte array if T is byte. If you create an ArrayList and add 10000 byte
entries, you'll end up with 10000 boxed bytes, taking up 12 bytes each
in 10000 separate objects. If you create a List<byte> and add 10000
bytes, you'll end up with a single array of bytes. In addition, you
won't incur boxing/unboxing runtime speed overheads either.
 

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