Reputation: 20129
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
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
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
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
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:
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
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
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