Property access modifier C#

Asked

Viewed 70 times

5

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

public string Codigo { get; private set; }

Or just ignore it:

public string Codigo { get; }

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

1 answer

8


The first form:

public string Codigo { get; private set; }

declares a property of public reading and private writing.

The second form:

public string Codigo { get; }

declares a property readonly public, as it is readonly it is only possible to start it in the manufacturer or during the declaration.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.