Re: Delphi to Visual Studio Conversion Project
- From: "Rudy Velthuis [TeamB]" <newsgroups@xxxxxxxxxxxx>
- Date: Sun, 30 Dec 2007 23:31:05 +0100
Anonymous wrote:
GC I can understand, though I dislike it myself, having seen machines
come to a dead stop while the GC did its thing, then resume again
(this was probably 20 years ago, so I'm sure it's improved greatly).
But other than GC, why choose C# over CBuilder? Is there an
advantage of generics over templates that I'm missing?
Yes: constraints. Templates use some kind of duck typing. I.e., you can
make your type do anythign conceivable (like call methods on it, or
apply operators like ">" and "==") and only at the time of
instantiation (i.e. when you finally assign a specific type to the
generic parameter) you find out that your type, which the template
expects to support "==" or ">" does not support it at all.
Templates are in fact really compiled at the moment they are used. So
the consumer of the template only find out if a type is usable wit the
tmplate when the specific instantiation is being compiled.
Constraints allow you to specify the capabilities of a type (e.g. that
they must implement IComparable, or IPrintable or whatever you can
conceive, and also if the type is a value type or a reference type, if
it must have a (default) constructor, etc.). This means you can perform
what the constraints specify, i.e. you can only cast the type to
IComparable if the constraint says so, etc.
When you compile the generics, all the capabilites are already tested
by the compiler. In C++, you can cast anything to IComparable and only
find out your class doesn't implement it if you start using some of the
methods. IN .NET generics, you find out as soon as you want to specify
the type.
So yes, there are huge differences. .NET generics are precompiled and
compiler checked before use. You can exactly state what your types must
be able to do, if that is required. You don't need the source of
List<T> before you can use it.
C++ templates allow everything at the time of writing, and are in fact
no more than glorified, type checked macros. You (generally) need the
source code.
--
Rudy Velthuis [TeamB] http://www.teamb.com
"They have computers, and they may have other weapons of mass
destruction." -- Janet Reno, Us Attorney General, 02-27-98
.
- Follow-Ups:
- Re: Delphi to Visual Studio Conversion Project
- From: Anonymous
- Re: Delphi to Visual Studio Conversion Project
- References:
- Delphi to Visual Studio Conversion Project
- From: Anonymous
- Re: Delphi to Visual Studio Conversion Project
- From: Joanna Carter [TeamB]
- Re: Delphi to Visual Studio Conversion Project
- From: Anonymous
- Delphi to Visual Studio Conversion Project
- Prev by Date: Re: One year CodeGear
- Next by Date: Re: Delphi to Visual Studio Conversion Project
- Previous by thread: Re: Delphi to Visual Studio Conversion Project
- Next by thread: Re: Delphi to Visual Studio Conversion Project
- Index(es):
Relevant Pages
|