In C# I have a generic class:
public class MyGeneric<ParameterClass> where ParameterClass: MyGenericParameterClass, new() {
public static int Variable;
}
Now in C++ if I instantiated a templated class with different parameters each complete class would get it’s own Variable, so I just can’t say
MyGeneric.Variable = 1; // invalid in C++
in C++, but seems like I can do so in C#.
I’d like to clarify…
If I have a generic with a static member variable is that variable shared among all generic instantiations?
Section 25.1.4 of the ECMA C# Language specification
You may see this blog post: Static fields in generic classes by Gus Perez
You can’t do that in C# as well.
Consider the following example from ECMA Language Specification.