0

I'm wondering if there is a difference between accessing private properties in C# through the automatic set and get properties and manually written GetSomeValue() methods.

Eg:

private int someValue { get; set; };

Vs.

public int GetSomeValue()
{
    return someValue;
}
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
  • 2
    Yes, there is: the syntax. BTW, `someValue` is not a private *property*, it is a private *field*. – dymanoid Mar 13 '20 at 15:44
  • Assuming you're meaning `print int someValue { get; set; }`? If so, no. If you were doing anything extra in the get/set then that'd be the difference. – rhys_stubbs Mar 13 '20 at 15:45
  • Properties and methods both can be virtual and declared in the interface. Actually each property is compiled as get and set methods in the IL. However `getComeValue()` method can be overridden as one more method with different set of parameters (or has default parameter) while property can't. It would be strange to have all properties replaced with methods in the source code (for C# world) – oleksa Mar 13 '20 at 15:50
  • *"I am wondering"* is rarely a good enough reason, but the worst is you didn't do any kind of [research](https://www.google.com/search?q=c%23+overhead+of+method+call). – Sinatr Mar 13 '20 at 15:55

1 Answers1

1

There really isn't any difference. The property syntax is largely just a shortcut to writing a dedicated getter/setter method. The compiler will create a Get and Set method as well as a private backing field when using properties.

When looking at the Intermediate language you will see that the generated code is basically identical minus some attributes marking compiler generated code and odd looking compiler generated naming conventions. In both cases a private field is accessed by a getter/setter function.

Take these two different implementations:

//method 1
public int Property {get; set;}

//method 2
int property;
public int GetProperty() { return property;}
public void SetProperty(int value) { property = value; }

In IL you'll see the following for the method 1 getter:

//compiler generated backing field
.field private int32 '<Property>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
    01 00 00 00
)

//compiler generated getter method
.method public hidebysig specialname 
    instance int32 get_Property () 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
    //retrieves a backing field
    IL_0001: ldfld int32 Test1.Program::'<Property>k__BackingField' 
    IL_0006: ret
} // end of method Program::get_Property

//your property
.property instance int32 Property()
{
    .get instance int32 Test1.Program::get_Property()
    .set instance void Test1.Program::set_Property(int32)
}

Then if you look at the IL for method 2:

.field private int32 'property'

.method public hidebysig 
    instance int32 GetProperty () cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 7 (0x7)
    .maxstack 8

    IL_0000: ldarg.0
    IL_0001: ldfld int32 Test1.Program::'property'
    IL_0006: ret
} // end of method Program::GetProperty
danielm
  • 339
  • 1
  • 8