What are the differences between Generics in C# and Java… and Templates in C++?
Hi all,
I am experienced C++ p
You'll get a better answer shortly, I'm sure. At that time, I'll delete this one.
The difference is that templates in C++ are similar to macros. It's when the template is instantiated that the code is compiled, and compilation errors are displayed if the implicit constraints are violated. That's how you can do template specializations - the template is basically already expanded by the specialization, so that's the one that's used.
Generics in .NET (also in VB.NET) are a runtime construct. They're a special kind of type. The constraints are necessary in order to ensure that any actual use of the type will be valid when the type is finally used.
You can actually use Reflection to look at a generic type and find the type parameters used to instantiate it, or look at a generic definition and see the constraints on each type parameter. In C++, this information is already gone at runtime.