471,309 Members | 1,075 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Are templates usefull?

Are they?
I mean, you could always downcast them for particular use, but then the
system would be well designed with intefaces instead of generics, and for
other uses I don't see any sense (collections excluded). Why do I need to
hold something of type 'object'? For what concrete usage? I am confused with
interfaces and generics... Please someone explain to me.
Jan 25 '06 #1
3 1109
luke,

Generics are a great way to get general functionality while exposing
specific types.

You have already mentioned the example of collections, for example.

With interfaces and generics, they are a way of specifying the specific
type to use for the contract that makes up the interface.

For example, you know that the IEnumerator interface is going to iterate
through a list/collection/whatever. In order to support all types, it just
returns an object, which you then have to cast. This isn't type safe, and
an erroneous cast will be caught at run-time, not compile time.

With IEnumerator<T>, you don't have to cast, because the Current
property returns T. If you try to misassign this to another variable, you
catch it at compile time.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"luke" <lu**@hotmail.com> wrote in message
news:dr**********@sunce.iskon.hr...
Are they?
I mean, you could always downcast them for particular use, but then the
system would be well designed with intefaces instead of generics, and for
other uses I don't see any sense (collections excluded). Why do I need to
hold something of type 'object'? For what concrete usage? I am confused
with interfaces and generics... Please someone explain to me.

Jan 25 '06 #2
> With IEnumerator<T>, you don't have to cast, because the Current
property returns T. If you try to misassign this to another variable, you
catch it at compile time.

Hope this helps.


Yes it does, thanks.
Jan 25 '06 #3

"luke" <lu**@hotmail.com> wrote in message
news:dr**********@sunce.iskon.hr...
With IEnumerator<T>, you don't have to cast, because the Current
property returns T. If you try to misassign this to another variable,
you catch it at compile time.

Hope this helps.


Yes it does, thanks.


But be aware that you can't just update all your old code because of at
least 1 big gotcha [it got me :-(] IList<T> does NOT derive from IList (and
there IS a good(ish) reason for this)

This causes a problem where a method must continues to take IList and you
are now trying to pass IList<T>
Jan 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Stijn Goris | last post: by
4 posts views Thread by Ives Steglich | last post: by
4 posts views Thread by John Goche | last post: by
5 posts views Thread by dj | last post: by
reply views Thread by rosydwin | last post: by

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.