Access modifier property C #

5

I noticed that it is possible to sign the access mode of a property as private:

public string Codigo { get; private set; }

Or just ignore it:

public string Codigo { get; }

Is there a difference or some scenario where one of these signatures should be used?

    
asked by anonymous 10.04.2016 / 17:29

1 answer

8

The first form:

public string Codigo { get; private set; }

declares a public read and private write property.

The second form:

public string Codigo { get; }

declare a public readonly property, such as readonly you can only start it in the constructor or during the declaration.

The second form guarantees external and internal immutability, the former only guarantees external immutability.

    
10.04.2016 / 17:43