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

后端 未结 4 2023
半阙折子戏
半阙折子戏 2020-12-11 15:11

In C# I have a generic class:

public class MyGeneric where ParameterClass: MyGenericParameterClass, new() {
    public static int Varia         


        
相关标签:
4条回答
  • 2020-12-11 15:45
    MyGeneric<MyClass>.Variable
    MyGeneric<MyOther>.Variable
    

    These two are different static variables treated like separate classes.

    0 讨论(0)
  • 2020-12-11 15:47

    No, it is not shared.

    Each MyGeneric<T> class will resolve to a different runtime type, for each possibility of T.

    Please check that there is no non-generic MyGeneric class with the Variable static member.

    0 讨论(0)
  • 2020-12-11 15:53

    No, it is not. Generic types can be "open" or "closed." An open type is one like List<T> where the type parameter isn't defined; List<int> is a closed type.

    Essentially, the open type isn't treated as a proper "Type" by the runtime - only the closed versions are true types. So, MyGeneric<int> and MyGeneric<string> are two entirely different types, and thus have their own instances of the static variable.

    This is made more obvious by the fact that you can't call your static member in the way you suggest: MyGeneric.Variable will not compile in C#.

    This console application code illustrates it quite simply:

    class Program
    {
        static void Main(string[] args)
        {
            Test<int>.i = 2;
            Test<string>.i = 8;
    
            Console.WriteLine(Test<int>.i);   // would write "8" if the fields were shared
            Console.WriteLine(Test<string>.i);
            // Console.WriteLine(Test.i); // does not compile
            // Console.WriteLine(Test<>.i); // does not compile
        }
    }
    
    class Test<T>
    {
        public static int i;
    }
    
    0 讨论(0)
  • 2020-12-11 15:56

    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 
        }
    }
    
    0 讨论(0)
提交回复
热议问题