Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a static member variable common for all C# generic instantiations?

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?

like image 976
sharptooth Avatar asked Feb 07 '13 10:02

sharptooth


People also ask

Are static variables global in C?

A static variable can be either a global or local variable. Both are created by preceding the variable declaration with the keyword static. A local static variable is a variable that can maintain its value from one function call to another and it will exist until the program ends.

Is C function static by default?

In C, functions are global by default. The “static” keyword before a function name makes it static.

Do static variables change in C?

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

Are variables in C static or dynamic?

In C, variables are always statically (or lexically) scoped i.e., binding of a variable can be determined by program text and is independent of the run-time function call stack. For example, output for the below program is 0, i.e., the value returned by f() is not dependent on who is calling it.


2 Answers

Section 25.1.4 of the ECMA C# Language specification

A static variable in a generic class declaration is shared amongst all instances of the same closed constructed type (§26.5.2), but is not shared amongst instances of different closed constructed types. These rules apply regardless of whether the type of the static variable involves any type parameters or not.

You may see this blog post: Static fields in generic classes by Gus Perez

You can't do that in C# as well.

MyGeneric.Variable = 1;

Consider the following example from ECMA Language Specification.

class C<V>
{
    static int count = 0;
    public C()
    {
        count++;
    }
    public static int Count
    {
        get { return count; }
    }
}
class Application
{
    static void Main()
    {
        C<int> x1 = new C<int>();
        Console.WriteLine(C<int>.Count);  // Prints 1 
        C<double> x2 = new C<double>();
        Console.WriteLine(C<double>.Count); // Prints 1 
        Console.WriteLine(C<int>.Count);  // Prints 1 
        C<int> x3 = new C<int>();
        Console.WriteLine(C<int>.Count);  // Prints 2 
    }
}
like image 149
Habib Avatar answered Oct 14 '22 15:10

Habib


MyGeneric<MyClass>.Variable
MyGeneric<MyOther>.Variable

These two are different static variables treated like separate classes.

like image 41
Michael Christensen Avatar answered Oct 14 '22 15:10

Michael Christensen