Reputation: 83
I've been trying to figure out which of these is illegal:
public int MyProperty { get; set;}
public int MyProperty { get; protected set; }
public int MyProperty { get; }
public int MyProperty { get{ return 0; } }
public int MyProperty { get { return 0; } set { ; } }
None of them returns an error for me but one of them is supposed to be illegal. Can someone tell me which one and why?
Upvotes: 0
Views: 75
Reputation: 5910
public int MyProperty { get; }
is illegal.
you could simply write
public int MyProperty { get; private set; }
to archive that
Upvotes: -1
Reputation: 186668
In C# 6.0 all are legal, in prior versions
public int MyProperty { get; }
is illegal - you can't assign (i.e. set
) the returned value. In C# 6.0, however, you can either put
public int MyProperty { get; } = 0;
or assign value in a constructor
public MyClass() {
MyProperty = 0;
}
Upvotes: 3