What is the difference in how these objects are created?

What is the difference between the two cases below:

class Data { PersonDataContext persons = new PersonDataContext(); public Data() {} } 

against

 class Data { PersonDataContext persons; public Data() { persons = new PersonDataContext(); } } 

I have the same question in asp.net:

 public partial class Data : System.Web.UI.Page { PersonDataContext persons = new PersonDataContext(); protected void Page_Load(object sender, EventArgs e) { } } 

against

 public partial class Data : System.Web.UI.Page { PersonDataContext persons; protected void Page_Load(object sender, EventArgs e) { persons = new PersonDataContext(); } } 
+6
source share
6 answers

Field initializers are started before the constructor is called . So, in case 1, your PersonDataContext object will be created before the constructor is called.

 PersonDataContext persons = new PersonDataContext(); 

In the second case, your constructor will execute first and then initialize the field.

You can do a simple test

 class PersonDataContext { public PersonDataContext() { Console.WriteLine("In PersonDataContext Constructor"); } } class Data { PersonDataContext persons = new PersonDataContext(); public Data() { Console.WriteLine("In Data Contructor"); } } class Data2 { PersonDataContext persons; public Data2() { Console.WriteLine("In Data2 Constructor"); persons = new PersonDataContext(); } } 

and then in the main method

 static void Main(string[] args) { Data d1 = new Data(); Data2 d2 = new Data2(); Console.Read(); } 

Exit

In the constructor of PersonDataContext // Case 1

In Data Contructor // Case 1

In the constructor of Data2 // Case 2

In the constructor of PersonDataContext // Case 2


For an example asp. In the first case, your field is initialized before the Page_Load event, and in the second case, your field is initialized in the Page_Load event

+3
source

In the first case, you initialize the variable on the announcement site.

In the second case, you initialize it in the constructor.

The main difference is that if you have additional constructors and you forgot to initialize or group the constructors, you can have an uninitialized variable.

+4
source

In your second example, the main difference is that Page_Load is just one of many ASP.NET events that are raised when a request is processed. As a result, events raised before Page_Load cannot access / use this field because it has not yet been initialized.

Check the life cycle of an ASP.NET page: http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx

+3
source

The only difference is the order in which objects are created. If I remember well, the field initializer starts before the constructor, so if the constructor refers to the field, it already exists.

But with 99.9% of the case, you will not see another between them. In most cases, it is safer to initialize your field directly on it; this will not allow you to forget it in the case of a multiple constructor or derived class and complex hierarchy.

Often, designers use external parameters to populate fields that cannot be with a field initializer.

+2
source

You will have an unintialized variable for second case if you use a constructor other than the default constructor to initialize the object -

 Data data = new Data(5); 

But if you are chain parametized constructors to call default constructor like this -

 class Data { PersonDataContext persons; public Data() { persons = new PersonDataContext(); } public Data(int a) : this() { } } 

the persons variable will be initialized.

0
source

In your first example, there will be no effective difference as long as the constructor is the only constructor. If you add other constructors, you need to make sure that they all initialize the face field.

In the ASP.NET example (for the second code snippet), you initialize the "face" field in the Page_Load method, which means that any other methods that use this field may be uninitialized. (Did you mean Page_Load, or did you want to make it a constructor ...?)

The bottom line indicates that if the "people" field should always be non-zero, it is safer to initialize it as a field initializer ...

0
source

All Articles