Constructors versus initializers in C #

Possible duplicate:
What is the difference between an object initializer and a constructor?

In C #, you can build an object like:

public class MyObject{ int val1; int val2; public MyObject(int val1, val2){ this.val1 = val1; this.val2 = val2; } } 

FROM

 MyObject ob = new MyObject(1,2); 

or using:

 MyObject ob = new MyObject(){ val1 = 1, val2 = 2 }; 

What is the difference between such constructors?

+7
source share
6 answers
 MyObject ob = new MyObject(){ val1 = 1, val2 = 2 }; 

is just syntactic sugar (i.e. shortening) for

 MyObject ob = new MyObject(); ob.val1 = 1; ob.val2 = 2; 

One of the two differences is that you can set readonly fields from the constructor, but without using the shortened version.

The second difference is that the constructor with parameters forces the client to provide these values. See Constructor Injection vs. Setter Injection for a good background reading.

+18
source

The difference is probably that the second will not compile.

You are missing the default constructor, which it calls in your second example.

+7
source
 MyObject ob = new Object(1,2); 

This is building an object and doing something with values ​​in the constructor. Perhaps it can set values, maybe it is not. It depends on the logic in the constructor.

 MyObject ob = new Object(){ val1 = 1, val2 = 2 }; 

This is building the object using the default constructor and setting the initial values ​​of the properties after the object was created. In this case, setting values ​​has nothing to do with the constructor, except that it is in the same expression. This is the same as:

 MyObject ob = new Object(); ob.val1 = 1; ... 

What is neat in this syntax is what you can do:

  Object ob = new Object(){ val1 = 1, val2 = 2 }; 

instead of doing:

 Object ob = new Object(1,2); ((MyObject)ob).val1 = 1; //note having to cast. ... 

because the initialization of variables occurs without touching the destination of the variable.

+4
source

Second form

  MyObject ob = new Object(){ val1 = 1, val2 = 2 }; 

- syntax

  MyObject ob = new Object(); ob.val1 = 1; ob.val2 = 2; 

The key point is that setting values ​​is not part of the constructor.

+1
source

The first parameter is ctor

 MyObject ob = new MyObject (1,2); 

The second parameter is ctor by default (null parameter), but as soon as ctor completes, val1 and val2 are initialized with the provided values

 MyObject ob = new MyObject (){ val1 = 1, val2 = 2 }; 

In addition, you can call the second without bindings. For example.

 MyObject ob = new MyObject { val1 = 1, val2 = 2 }; 
+1
source

Both are not the same.

This calls the constructor of the class:

 MyObject ob = new Object(1,2); 

And this is the inline object initializer:

 MyObject ob = new Object(){ val1 = 1, val2 = 2 }; 

First, it ensures that creating an instance of MyObject requires two integer input parameters at build time, and the other only initializes some fields or properties after the build time. The latter retains the freedom of not necessarily initializing such properties and / or fields.

+1
source

All Articles