Here are 3 scenarios:
namespace NS
{
public partial class A: System.Web.UI.UserControl
private Variable v;
protected void Page_Load(object sender, EventArgs e){
if (!Page.IsPostBack) v= new Variable();
....
}
}
namespace NS
{
public partial class A: System.Web.UI.UserControl
private Variable v = new Variable();
protected void Page_Load(object sender, EventArgs e){
}
}
namespace NS
{
public partial class A: System.Web.UI.UserControl
private Variable v;
protected void Page_Load(object sender, EventArgs e){
v = new Variable();
}
}
When does the variable “v” gets created every time for the 2nd scenario? Is the 2st scenario is equivalent to the 3rd one?
Scenario 1:
the variable v is intialized on every request, when page load happens, and there is no post back. (otherwise null)
scenario 2:
the variable v is initialized on every instantiation of the class A, bevore the constructor is called.
scenario 3:
the variable v is intialized on every request, when page load happens.
comment:
if you access the variable v only after page load happens, then scenario 2 & 3 can be treated equal.