Michael Haddad
Michael Haddad

Reputation: 4445

Backing field vs "value" keyword in property setter

I have a property with a backing field and some logic inside the setter. I wonder whether I should use the value keyword or the backing field.

Option 1:

private bool _backingField;
public bool MyProperty
{
    get => _backingField;
    set
    {
        _backingField = value;
        if(value) // <--
        {
            ...
        }
    }
}

Option 2:

private bool _backingField;
public bool MyProperty
{
    get => _backingField;
    set
    {
        _backingField = value;
        if(_backingField) // <--
        {
            ...
        }
    }
}

Which of them has better performance? Tests I have run on my machine showed no significant difference, but I am not sure my machine is enough to get the whole picture.

Note: I do realize this is probably taking micro-optimization to a whole new level of ridiculousness, but I am still curious to know if there is a definitive answer.


Edit: This question is not opinion-based, since I am asking if there is an objective difference.

Upvotes: 0

Views: 257

Answers (1)

Here's the IL generated with if(value) in Release mode:

.method public hidebysig specialname instance void 
        set_MyProperty(bool 'value') cil managed
{
  // Code size       21 (0x15)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldarg.1
  IL_0002:  stfld      bool ConsoleApp1.Program::_backingField
  IL_0007:  ldarg.1
  IL_0008:  brfalse.s  IL_0014
  IL_000a:  ldstr      "true"
  IL_000f:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0014:  ret
} // end of method Program::set_MyProperty

Note that I've added Console.WriteLine("true"); to the body of the if to prevent it to be removed by the compiler.

Now the IL with if (_backingField):

.method public hidebysig specialname instance void 
        set_MyProperty(bool 'value') cil managed
{
  // Code size       26 (0x1a)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldarg.1
  IL_0002:  stfld      bool ConsoleApp1.Program::_backingField
  IL_0007:  ldarg.0
  IL_0008:  ldfld      bool ConsoleApp1.Program::_backingField
  IL_000d:  brfalse.s  IL_0019
  IL_000f:  ldstr      "true"
  IL_0014:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0019:  ret
} // end of method Program::set_MyProperty

The only difference is the additional call to ldfld bool ConsoleApp1.Program::_backingField in the second version, so in theory it should be a tick slower. However, that tick should be negligible small.

Upvotes: 2

Related Questions