Search code examples
c#propertiesprivate

Properties and private set


Why does this code not work?

class Test
{
    int Abc { private set; get; }
}

What is the default access modifier for properties?


Solution

  • 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.