Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Generics vs C++ Templates
C# Generics and C++ Templates both provide support for parameterized types, allowing you to write reusable code that works with different data types. However, they differ significantly in their design philosophy, capabilities, and implementation approaches.
Key Differences Between C# Generics and C++ Templates
| Feature | C# Generics | C++ Templates |
|---|---|---|
| Compilation Model | Compile-time and run-time support | Compile-time only |
| Type Safety | Strong type checking at compile-time | Duck typing, errors at instantiation |
| Specialization | No explicit or partial specialization | Full support for specialization |
| Non-type Parameters | Not supported | Fully supported |
| Default Types | Not allowed | Supported |
| Inheritance | Type parameters cannot be base classes | Type parameters can be base classes |
Compilation Model Differences
C++ Templates use a compile-time model where template code is expanded at each instantiation point. C# Generics maintain type information at runtime, providing better runtime support and reflection capabilities −
using System;
// C# Generic class
public class GenericContainer<T> {
private T value;
public GenericContainer(T val) {
value = val;
}
public void PrintType() {
Console.WriteLine("Type: " + typeof(T).Name);
Console.WriteLine("Value: " + value);
}
}
class Program {
public static void Main() {
GenericContainer<int> intContainer = new GenericContainer<int>(42);
GenericContainer<string> stringContainer = new GenericContainer<string>("Hello");
intContainer.PrintType();
stringContainer.PrintType();
}
}
The output of the above code is −
Type: Int32 Value: 42 Type: String Value: Hello
Type Safety and Constraints
C# Generics provide stronger type safety through constraints, while C++ Templates rely on duck typing −
using System;
// Generic class with constraint
public class Calculator<T> where T : IComparable<T> {
public T Max(T a, T b) {
return a.CompareTo(b) > 0 ? a : b;
}
}
class Program {
public static void Main() {
Calculator<int> intCalc = new Calculator<int>();
Console.WriteLine("Max of 5 and 8: " + intCalc.Max(5, 8));
Calculator<string> stringCalc = new Calculator<string>();
Console.WriteLine("Max of 'apple' and 'banana': " + stringCalc.Max("apple", "banana"));
}
}
The output of the above code is −
Max of 5 and 8: 8 Max of 'apple' and 'banana': banana
Specialization Limitations
Unlike C++ Templates, C# Generics do not support explicit or partial specialization. You cannot create specialized versions for specific types −
// This is NOT possible in C#
public class MyGeneric<T> {
public void Process(T item) { /* general implementation */ }
}
// Explicit specialization - NOT SUPPORTED in C#
public class MyGeneric<string> {
public void Process(string item) { /* specialized for string */ }
}
Non-Type Parameters
C# Generics do not support non-type template parameters, which are common in C++ for compile-time constants −
// This C++ style is NOT supported in C#
// template<int N> class Array { ... }
// C# alternative using constructor parameter
public class FixedArray<T> {
private T[] array;
public FixedArray(int size) {
array = new T[size];
}
}
Flexibility Comparison
Conclusion
C# Generics prioritize type safety and runtime support over flexibility, while C++ Templates offer maximum compile-time flexibility with features like specialization and non-type parameters. Choose C# Generics for type-safe, maintainable code with runtime reflection capabilities, and C++ Templates when you need maximum compile-time flexibility and performance optimization.
