简体   繁体   中英

Property declaration code being executed after page load in ASP.Net web form?

I have the following scenario.

I have a page class that has some properties.

Here is my class

public partial class p : System.Web.UI.Page
{
        private int? X
        {
            get
            {
                return // some helper method will return the value of X;
            }
        }
}   
protected void Page_Load(object sender, EventArgs e)
{
  // in the page load event
   var d = X;
}

If I put a break point in the get of the property and in the var d = X line.

The page load event is executing before the get helper method calling ?

I am trying to understand why this is happening ?

Thanks.

A getter/setter, in essence, is nothing different than a method call, just a shorthand syntax. Your code would logically be the same as

public partial class p : System.Web.UI.Page
{
    private int? GetX()
    {
        return // some helper method will return the value of X;
    }
}   
protected void Page_Load(object sender, EventArgs e)
{
  // in the page load event
   var d = GetX();
}

The getter will not save any value or do anything than return whatever the code specifies whenever it is called. Does that make sense to you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM