3

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

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

9 Comments

It is worth noting that generics will mean the static belongs to the type with the generic argument taken into account also.
@AdamHouldsworth - True. A static defined on a generic type will be shared between instances of the closed constructed generic type.
unclear downvote, by the way, may be more correct would be to say: "static belongs to a type".
@KamilLach - so long as they are not decorated as ThreadStatic.
@Oded They could have been instance variable of a class. See my edit
|
4

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.

3 Comments

Good to know, anyway that is perfectly logic since each instance of a generic class with different type parameter will yield object with different type.
Interesting solution, although I don't really like it since it is not very obvious what is going on. I don't want to puzzle the guy who is going to maintain my code behind me. So I have decide to add an abstract property and "redefine" in each subclass a static variable.
@mathk I'd personally never suggest actually using this solution, it is one giant code smell. If I knew what you wanted to achieve I could provide a more fitting solution, but as it stands, none of your questions have been very clear.
1

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 { } 

2 Comments

Thanks for the suggest. There is also an other downside, is that ClassInstVar can only be a instance side property. It can not be class side ("aka kindof static")
@mathk Now I sort of possibly might understand what it is you want, I have amended my answer with an alternative to this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.