Property with private set

2

Is there a difference between declaring the set private or just omit it?

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

Are the two lines of code equivalent?

    
asked by anonymous 03.02.2018 / 12:14

2 answers

2

They are not equivalent in this line:

public int UmaPropriedade {get; private set;}

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

Already in this line:

public int OutraPropriedade {get;}

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

    
03.02.2018 / 12:21
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

See in SharpLab .

Both allow publicly to take the value, but there is one 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, so either you should either use an initializer or you should boot into the constructor, otherwise it does not make sense to have such a property. Not that the first form makes sense, in most cases, without a boot.

    
03.02.2018 / 12:37