C # property evaluation time

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 of access to Foo? A debugger that evaluates all the properties scares me.

+3
source share
2 answers

Yes, the property is just syntactic sugar for calling the getaccessor method . Each time a property is read, the method is executed. And yes, this includes a debugger (therefore, if your gettors properties have side effects, debugging can actually affect the way your program works).

+8
source

Yes, C # properties are just syntactic sugar for specialized methods, so they are evaluated when called like any other instance method.

+2
source

Source: https://habr.com/ru/post/1713507/


All Articles