This is where the difference between C++ templates and C# generics comes into play.
In C++, templates cause a new method to be generated for each type it is used with. In C#, however, the code in the method is only created once. The same code runs whether you call it with an int
, string
, or object
type parameter.
Because C# generics remain generic when compiled, they can be exposed in compiled libraries without need for re-compilation. In C++, you are required to include the original template in your consuming code, so a new copy can be compiled.
Simply put, you only get one compiled method per generic method.