How are Generics of C# (C Sharp) Different from C++ Templates

C# Generics and C++ Templates share a commonality that they deal with types that are parameterized. You need not define the type readily; it can be determined and supplied at a later point of time. Though their intension is same, they both have a couple of differences as well. This article will give you an overview on the differences between C# generics and C++ templates.

Given below is a chart highlighting the difference between C# generics and C++ templates. The chart contains list of tasks. If the task is applicable to either C++ templates or C# Generics, a tick mark is shown in the appropriate column. If the task is not applicable, then a cross mark is shown.

Task C++ Templates C# Generics
Usage of Operators (Both Arithmetic and User Defined) ? X
Explicit Specialization ? X
Partial Specialization ? X
Default Types Associated with Parameterized Type ? X
Define and Use Template Parameters ? X
Define and Use Non-template Parameters ? X
Use Lazy Structural Constraints ? X
Directly Inherit From Type Parameter ? X
Use Reflection to Provide Run Time Support X ?
Strong Type Checking X ?
Define and Enforce Constraints on Parameterized Type X ?
Mention Valid Operations at the Time of Definition X ?
Display of Better Error Messages X ?
Option to Specify Parameterized Type to Implement a Specific Interface X ?
Flexibility to Use in a Cross Library and Cross Language Fashion X ?

Other Differences:

C++ Templates C# Generics

Instantiation is done at compile time. Instantiation is done at run time.
In C++ templates, each type associated with the template will have an individual class definition irrespective of whether it is an object type or value type. At the time of compilation, all the object types share a single class definition and an independent class definition is provided for each of the value type.

When you instantiate C++ template, during compilation time entire content of the template will be copied in the place of instantiation. If ten instances of the template are used in different places of code, then ten copies of the template code will be pasted in those places. No such textual expansion happens.

You cannot create any dll or reusable component containing these templates which can be reused at a later point of time. You can create generic dll containing generic class. This generic dll can be used across various projects.

C++ templates are similar to macros. Both undergo textual expansion during compilation. C# Generics are similar to classes except that generics accept parameterized types whereas classes don’t.

Type identity of specialization is unique for each assembly. C# Generics provide globally unique type identity of specialization.

Name lookup is done at the time of specialization. Name lookup is done at the time of definition.Binding to type. Binding to constraints.


Add to My Yahoo!

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.


Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage


Recommended Resource

| AppDomain class in .Net | Asynchronous programming in .Net | Better data transmission using Compressed Streams in .Net | Encoding and Decoding Data in .Net | Handling Unmanaged Code in .Net | How are Generics of C# Different from C++ Templates | How Do You Establish Variance in Generic Typesm: Reference Types | How Do You Identify Nullable Type in C#m: Reference Types | How Do You Perform Boxing On Nullable Types in C# |Overview of Code Access Security in .Net |

| Privacy Policy for | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.