2

On a webforms .aspx page system, the master page has a few properties auto initialized, as such

public bool MyProp => bool.Parse(Service.Settings["YorN"]);

Profiling page load, I see that between the PreRender event, and the initialization of one of the properties there is a large gap.

Where can I look to figure out the delay? What runs between the two?

JNF
  • 3,696
  • 3
  • 31
  • 64

3 Answers3

9

That is not an auto property. That's an expression bodied member.

Your implementation of MyProp computes bool.Parse(Service.Settings["YorN"]); every time the property getter is called. So in your case, that code is run whenever MyProp is called, and it's run each time it's called.

If you used an auto property, which would be

public bool MyProp {get;} = bool.Parse(Service.Settings["YorN"]);

Then it would be run after the instance is created, and just before the constructor is called (when other field initializers run). Note that, as this code runs in a field initializer, it cannot use the implicit reference (this) so if Service is an instance variable, this won't compile.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Isn't it _after_ the constructor is called? – JNF Nov 23 '16 at 16:59
  • @JNF: It would be before the constructor *body* executes, at the same time as other field initializers. – Jon Skeet Nov 23 '16 at 17:00
  • @JNF No, field initializers run before the constructor. Create an instance of this class to see for yourself: `public class Foo { int i = 5; public Foo() { Console.WriteLine(i); } }` If the field is initialized first it'll print `5`, if it's not, it'll print `0`. – Servy Nov 23 '16 at 17:00
  • @jonSkeet, the compiler actually puts it at the beginning of the constructor? – JNF Nov 23 '16 at 17:04
  • @JonSkeet Just so I get my terminology right, how would you distinguish what someone calls to create an instance of the object (i.e. `new Foo();`) from the definition in that type of what's run on construction (i.e. `public Foo(){this.Something = initialValue;}`)? Normally you'd call both the constructor, but technically the user is only defining a portion of what happens in construction. When you need to distinguish between the two, what do you call each (and which one is *technically* the constructor)? – Servy Nov 23 '16 at 17:05
  • @JNF It's easiest to just run the code and see for yourself. But yes, it does. – Servy Nov 23 '16 at 17:06
  • @servy, read that somewhere, I'll see if I can find it again. – JNF Nov 23 '16 at 17:09
  • @Servy, of course, but we got sidetracked, it's not the actual issue here – JNF Nov 23 '16 at 17:14
  • 2
    @Servy: I don't believe there's any good terminology here, unfortunately. It's one of those points of ambiguity. The other aspect where there's a dichotomy is that often people consider a constructor to be "something that creates an instance" - whereas really it's "something that initializes an existing instance"... it's just that the "constructor call" first creates an object then starts calling the constructor chain. Sorry not to have better answers :( – Jon Skeet Nov 23 '16 at 17:16
1

Technically that's an expression-bodied member, which only gets called when you try to "get" the property. So it's called whenever it's asked for.

It's the equivalent of:

public bool MyProp 
{ 
    get 
    { 
         return bool.Parse(Service.Settings["YorN"]);
    }
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

They don't, properties are like methods in this case when they have expression bodies - their contents are evaluated when you try to read from them.

So every time something tries to read the value of MyProp it runs that expression.

What you probably want to do is have that as a read-only property and set it during your classes constructor.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62