mathk
mathk

Reputation: 8123

static variables are they class-instance variables?

Simple question:

Are static variable being class instance variable or class variable bases?

Knowing that class instance variable are variables that are define for each class and subclass where it is define. And class variable are variables that are global to all subclass where it is define including itself.

EDIT: Knowing that I am choking a lot of C#-ish guy I am using the term class instance as if a class where an instance of some MetaClass. This simplify greatly my question. Although it is not totally wrong to say that if you consider that the VM certainly have an artifact that represent evrey class (containing the method dictionay, instance size, the superclass, ...). Thanks

Upvotes: 3

Views: 2637

Answers (3)

Adam Houldsworth
Adam Houldsworth

Reputation: 64467

Static variables are scoped to the type they are defined in for a given AppDomain. They are also shared across threads, unless you use the ThreadStaticAttribute, at which point they become per thread.

Class members are obviously scoped to an instance of the class, but are not "global" to derived classes. Depending on the access modifier the member may be visible to derived instances also.

Classes with generic arguments have a static variable per closed generic type:

class MyClass<T>
{
   public static string Name;
}

So MyClass<int> will have its own copy of Name and MyClass<string> will have a different copy.


Looking at your choice of answer, it seems like you want a static variable per derived class?

You can cheat and use the generics rule above:

class Program
{
    static void Main(string[] args)
    {
        Derived1.WhatClassAmI = "Derived1";
        Derived2.WhatClassAmI = "Derived2";

        Console.WriteLine(Derived1.WhatClassAmI); // "Derived1"
        Console.WriteLine(Derived2.WhatClassAmI); // "Derived2"

        Console.WriteLine(BaseClass<Derived1>.WhatClassAmI); // "Derived1"
        Console.WriteLine(BaseClass<Derived2>.WhatClassAmI); // "Derived2"
        Console.Read();
    }

    class BaseClass<T> where T : BaseClass<T>
    {
        public static string WhatClassAmI = "BaseClass";
    }

    class Derived1 : BaseClass<Derived1>
    {
    }

    class Derived2 : BaseClass<Derived2>
    {
    }
}

They use the "same" static, but each have their own values due to the type closure.

Upvotes: 4

Francesco Baruchelli
Francesco Baruchelli

Reputation: 7468

They are class variables. There is nothing like a Smalltalk class instance variable in C#. I.e. there isn't a way to define a variable that is common across all the instances of a class, but which has different values for its subclasses.

To obtain a "similar" behaviour, but with the drawback that the class instance var is accessible only after having created an instance of your class I've done something like this:

public class BaseClass
{
    private static Dictionary<Type, object> ClassInstVarsDict = new Dictionary<Type, object>();

    public object ClassInstVar
    {
        get
        {
            object result;
            if (ClassInstVarsDict.TryGetValue(this.GetType(), out result))
                return result;
            else
                return null;
        }
        set
        {
            ClassInstVarsDict[this.GetType()] = value;
        }
    }
}

public class DerivedClass1 : BaseClass
{
}

public class DerivedClass2 : BaseClass
{
}

Upvotes: 1

Oded
Oded

Reputation: 498904

Static variables "belong" to the type - they are not instance variables.

That is, they are shared between all instances of the type, including generic closed constructed types.

The exceptions are static variables decorated with ThreadStatic, making the variable unique within a thread.

Upvotes: 9

Related Questions