Property with private set

Asked

Viewed 123 times

1

There is a difference between declaring the set private or simply omitting it?

public int UmaPropriedade {get; private set;}
public int OutraPropriedade {get;}

The two lines of code are equivalent?

2 answers

4

Let’s see how this code is generated:

.class private auto ansi '<Module>'
{
} // end of class <Module>

.class public auto ansi beforefieldinit C
    extends [mscorlib]System.Object
{
    // Fields
    .field private int32 '<UmaPropriedade>k__BackingField'
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )
    .field private initonly int32 '<OutraPropriedade>k__BackingField'
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )

    // Methods
    .method public hidebysig specialname 
        instance int32 get_UmaPropriedade () cil managed 
    {
        .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x2050
        // Code size 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldfld int32 C::'<UmaPropriedade>k__BackingField'
        IL_0006: ret
    } // end of method C::get_UmaPropriedade

    .method private hidebysig specialname 
        instance void set_UmaPropriedade (
            int32 'value'
        ) cil managed 
    {
        .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x2058
        // Code size 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldarg.1
        IL_0002: stfld int32 C::'<UmaPropriedade>k__BackingField'
        IL_0007: ret
    } // end of method C::set_UmaPropriedade

    .method public hidebysig specialname 
        instance int32 get_OutraPropriedade () cil managed 
    {
        .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x2061
        // Code size 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldfld int32 C::'<OutraPropriedade>k__BackingField'
        IL_0006: ret
    } // end of method C::get_OutraPropriedade

    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
    {
        // Method begins at RVA 0x2069
        // Code size 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [mscorlib]System.Object::.ctor()
        IL_0006: ret
    } // end of method C::.ctor

    // Properties
    .property instance int32 UmaPropriedade()
    {
        .get instance int32 C::get_UmaPropriedade()
        .set instance void C::set_UmaPropriedade(int32)
    }
    .property instance int32 OutraPropriedade()
    {
        .get instance int32 C::get_OutraPropriedade()
    }

} // end of class C

Behold in the Sharplab.

Both allow you to publicly take the value, but there is an important difference, the first allows the class to change its value, but not publicly, the second does not allow the value to be changed in any way, effectively it is read-only, Therefore, either you should use an initializer or you should initialize in the constructor, otherwise it makes no sense to have a property like this. Not that the first form makes sense, in most cases, without an initialization.

2


Are not equivalent in that row:

public int UmaPropriedade {get; private set;}

The value of the property UmaPropriedade can only be changed by the class itself, nor can the child classes set this property.

Already on this line:

public int OutraPropriedade {get;}

There is no method set, then no one can set anything on this property.

  • "(...) no one can set anything on this property (...)"?

Browser other questions tagged

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