C# FAQ: Are .NET generics like Cplusplus templates

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

Are .NET generics like C++ templates?

No, .NET generics are not like C++ templates.

Although .NET 2.0 generic types share a similar syntax with C++ templates, there are substantial differences. Whereas .NET generic types are instantiated at runtime, C++ templates are created at compile time. Significantly, .NET generic types can be inspected using reflection of metadata. And, generic member access on the type paramater is verified from the constraints placed on the type parameter. On the other hand, template member access is verified on the type argument after instantiation. Generally, depending upon the implementation, template code is faster and leaner than an equivalent generic alternative.

Beginning with Visual Studio 2005, C#, Managed C++, and Visual Basic .NET all have Common Language Runtime (CLR) support for generics.

See also



Personal tools