What are the differences between Generics in C# and Java… and Templates in C++?
Hi all,
I am experienced C++ p
Template and generics are really a different thing. One of the goals of generics is to be able to use them in a cross library, cross language way, which is not the same as C++ templates. They are a CLR concept, not a language concept (although they need language support obviously).
In C++, templates can be seen in a "macros on steroids" kind of way (no flame please, I know templates are not macros), because you can see them a as a textual expansion which is then compiled. This gives them the ability to use whatever is defined on the template parameter (mainly operators for example) because constraints are imposed by the code using them.
In .NET, since generics are resolved (instantiated) at runtime, constraint have to be imposed at the definition level so that the compiler can ensure their usage will be valid (which means that you cannot use operators on generics parameters because you cannot indicate a constraint for the existence of an operator).
As I said, the main point of generics is to be able to create generic dll to be used by other projects. This is why they are different.