473,513 Members | 2,684 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1175
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
2868
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
1562
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
360
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
1790
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
2951
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
7257
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
7157
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
7379
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,...
0
7535
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...
1
7098
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...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
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...
0
4745
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.