Why Can't Class Fields Be Var

Why can't class fields be var?

Eric Lippert answered your question right here: Why no var on fields?

Basically, for the general case it would require re-writing the C# compiler, as the way it currently does type inference would not work for cycles of var field variable assignments.

var in class gives error


// method variable
var X;

is never valid - even inside a method; you need immediate initialization to infer the type:

// method variable
var X = "abc"; // now a string

As for why this isn't available for fields with a field-initializer: simply, the spec says so. Now why the spec says so is another debate... I could check the annotated spec, but my suspicion would be simply that they are more necessary for method variables, where the logic is more complex (re LINQ etc). Also, they are often used with anonymous types (that being the necessity for their existence); but anonymous types can't be exposed on a public api... so you could have the very confusing:

private var foo = new { x = 123, y = "abc"}; // valid
public var bar = new { x = 123, y = "abc"}; // invalid

So all in all I'm happy with the current logic.

Why can't i declare a field using var

Straight from the source:

Why no var on fields?

Why can't variables or fields be declared in interface + c#

Lets say that it can be defined. So:

interface Foo
{
int Number;
string Text;
}

class Bar : Foo
{
public int Number;
public string Text;
}

So, in each derived class (class that implements Foo interface) you would have to create two public members. That, at least to me, makes no sense.

If you want your classes to have some members that are not methods, and you would like to simplify it as much as possible, take a look at Auto-Implemented Properties.

Why method can't access class variable?

It's important to understand that some of these comments are not equivalent. MyClass.a is a member of the class itself, self.a is a member of the instance of the class.

When you use self.a it will return a from the class, because there is no a on the instance. If there was also an a which was a member of the instance, it would return that instead. Generally the instance a is set using the __init__ constructor. Both of these can exist simultaneously.

class MyClass1:
a = 25

def __init__(self):
self.a = 100

def instance_a(self):
print(self.a)

def change_instance_a(self):
self.a = 5

def class_a(self):
print(MyClass1.a)

def change_class_a(self):
MyClass1.a = 10

# Create two instances
ins1 = MyClass1()
ins2 = MyClass1()

# Both instances have the same Class member a, and the same instance member a
ins1.instance_a()
ins2.instance_a()
ins1.class_a()
ins2.class_a()

# Now lets change instance a on one of our instances
ins1.change_instance_a()

# Print again, see that class a values remain the same, but instance a has
# changed on one instance only
print()
ins1.instance_a()
ins2.instance_a()
ins1.class_a()
ins2.class_a()

# Lets change the class member a on just one instance
ins1.change_class_a()

# Both instances now report that new value for the class member a
print()
ins1.instance_a()
ins2.instance_a()
ins1.class_a()
ins2.class_a()


Related Topics



Leave a reply



Submit