Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluation time of C# properties

Tags:

c#

properties

class Bar
{
  private byte[] dg;

  Bar(byte[] datagram)
  {
    dg = datagram;
  }

  int Foo
  {
    get { return BitConverter.ToInt16(dg, 8); }
  }
}

When are properties evaluated? At the time Foo is accessed? The debugger evaluating all properties is scaring me.


1 Answers

Yes, a property is just syntactic sugar for the call to get accessor method. Every time property is read, the method executes. And yes, this does include the debugger (which is why if your property gettors have side effects, debugging can actually affect the way your program works).

like image 83
Pavel Minaev Avatar answered Jan 01 '26 14:01

Pavel Minaev