473,388 Members | 995 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,388 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 1171
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stijn Goris | last post by:
hi all, I m designing my first 'real' site and want to do things proper this time. Using dreamwaever and php I was wondering what u guys think of templates in conjunction with php? Do you think...
1
by: Thomas Parslow | last post by:
Hi, I'm using an ASPTemplate class (the one from asptemplate.sourceforge.net) to produce WAP and HTML verions of the same pages. It seemed to me that it would be very usefull to have the...
4
by: Ives Steglich | last post by:
hi there i have a line like: boost::bind(&EventSource<SinkType>::fire<Op>,boost::ref(fireProc),_1) and this generates an error like: eventsource.h: In member function `void...
4
by: John Goche | last post by:
Hello, I was wondering what the pros and cons are for using numbers in templates rather than in constructors as in: MyBuffer<10foo; rather than MyBuffer foo(10);
5
by: dj | last post by:
I somehow understand why member function templates cannot be virtual, but my question is how I could achieve something similar to this: class A { public: template<typename Tvirtual f() = 0; }...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
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,...

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.