I know in C++, generics don't actually exist, but you can simulate it with the use of template
. When you build your code, the compiler pre-processes the code and generates a new code with the generic values replaced for the actual values that were specified in the object declaration and then is this new code which is really compiled. For instance, let's say we have the class A
as follows:
template<class T>
class A
{
T f();
};
and then somewhere else in the code we have A<int> a;
. The actual code that is compiled would be:
class A
{
//Replaces T by int in the pre-processing
int f();
};
After this whole introduction, lets get to the point.
My questions are:
- Does C# treats generics the same way as C++? If not, how then?
- Are they special types?
- Are they resolved in run-time or in compilation-time?
- How much space is reserved for generic types in the Activation Register?