Why does this code not work?
class Test
{
int Abc { private set; get; }
}
What is the default access modifier for properties?
Why does this code not work?
class Test
{
int Abc { private set; get; }
}
What is the default access modifier for properties?
The Abc property must be public, protected or internal:
public int Abc { get; private set; }
In your case the property is private (because you haven't specified an access modifier) so it's already a private set. You cannot modify its value outside of the current class so it doesn't really make sense to declare a private setter in this case.
Default access modifier for properties is private, as for any other member of a class. If you wish to make the setter less accessible you would need to make the property more accessible first and then put restriction on the setter.
class Test
{
public int Abc1 { private set; get; }
protected int Abc2 { private set; get; }
internal int Abc3 { private set; get; }
protected internal int Abc4 { private set; get; }
}
The default accessibility of all class members (including properties) is private
; see Accessibility Levels. The private
before your set
is redundant, thus the error. Your code would be semantically equivalent to the following:
class Test
{
int Abc { get; set; }
}
You only need to specify a private
access modifier for your set
accessor when the property is more accessible; for example (a common scenario):
class Test
{
public int Abc { get; private set; }
}