What does this .net constructor syntax mean?

I'm new to .NET and I just saw this code. The stuff in the {} is not syntax I've seen before, so I'm not entirely sure what it is.

var action = new DynamicAction(txnType, schema)
                             {
                                 ThreadMode = ThreadMode.GUI,
                                 IsMultipleSelectionAllowed = false
                             };

It looks like it's just assigning member variables, but then why not just do it normally with the rest of the constructor?

Upvotes: 0

Views: 97

Answers (6)

Rahul
Rahul

Reputation: 77896

This is known as Object Initializer and introduced it was introduced in the C# 3 compiler in which shipped with .NET 3.5.

Although object initializers can be used in any context, they are especially useful in LINQ query expressions. Query expressions make frequent use of anonymous types which can only be initialized by using an object initializer, as shown in the following declaration.

var pet = new { Age = 10, Name = "Fluffy" };

With your code

var action = new DynamicAction(txnType, schema)
 {
     ThreadMode = ThreadMode.GUI,
     IsMultipleSelectionAllowed = false
 };

It's creating the instance and assigning values to the properties ThreadMode and IsMultipleSelectionAllowed.

Internally (under the cover) runtime create the instance like below

DynamicAction tempobj = new DynamicAction(txnType, schema);
tempobj.ThreadMode = ThreadMode.GUI;
tempobj.IsMultipleSelectionAllowed = false;
action = tempobj;

so that, we don't ended up with unconstructed (OR) halfway constructed object which will not be useful in the application (if any runtime exception occurs while creation of the object).

Upvotes: 0

geek
geek

Reputation: 616

It's worth to know that object initializer is done after constructor.

For example if you perform code like this:

public class Test
{
    public string Name { get; set; }

    public Test(string name)
    {
         Name = name;
    }
}
...
var t = new Test("Name1") {Name = "Name2"};
Console.WriteLine(t.Name);

The result will be "Name2"

Upvotes: 2

theferrit32
theferrit32

Reputation: 241

A constructor is not always equipped to handle every single possible parameter that can be modified in an object, just commonly used, or important required cases, or cases that require special treatment inherent to the state of the object. If there is a field that is public in an object, it is possible to construct the object, then set individual field values to what you need. It is just (arguably) an easier way to initially define an object with all the values you need all in one statement without individually setting each later on, when the fields may not be parametrized by the object's constructor

Upvotes: 0

DocMax
DocMax

Reputation: 12164

Yes, it is assigning properties in construction and is equivalent to:

var action = new DynamicAction(txType, schema);
action.ThreadMode = ThreadMode.GUI;
action.IsMultipleSelectionAllowed = false;

This approach can be used when:

  1. You don't have control over the constructor and cannot add additional parameters to it.
  2. You don't want to pass a long list of parameters to the construction each call and instead only set the ones you care about.

This is particularly useful if you are not assigning the object to a local variable as in:

SomeMethod(new DynamicAction(txType, schema) { 
    action.ThreadMode = ThreadMode.GUI,
    action.IsMultipleSelectionAllowed = false }
);

In this case, with the object initializer, there is no need to create a temporary local variable.

Upvotes: 5

Luis Tellez
Luis Tellez

Reputation: 2983

Its the Object Initializer, you use any constructor and then assign the values of the properties you need to change.

You can refer to the Microsoft documentation about it at :

http://msdn.microsoft.com/en-us/library/bb384062.aspx

In your case you have a constructor with 2 parameters, and then initialize the other 2.

Upvotes: 4

Mark Seemann
Mark Seemann

Reputation: 233247

It's called an Object Initializer. In this example:

var action = new DynamicAction(txnType, schema)
{
    ThreadMode = ThreadMode.GUI,
    IsMultipleSelectionAllowed = false
};

it's used to assign values to the ThreadMode and IsMultipleSelectionAllowed properties.

It was introduced together with LINQ, because it's more convenient to be able to create a new object and assign properties to it in a single operation when you do projections:

var foos = bars.Select(b => new Foo { Baz = b.Qux, Corge = b.Grault });

Upvotes: 3

Related Questions