Possible Duplicate:
Why are C# 3.0 object initializer constructor parentheses optional?
What is the difference between instatiating an object by using
classInstance = new Class() { prop1 = "", prop2 = "" };
and
classInstance = new Class { prop1 = "", prop2 = "" };
Initializer. In C/C99/C++, an initializer is an optional part of a declarator. It consists of the '=' character followed by an expression or a comma-separated list of expressions placed in curly brackets (braces).
What is the difference between initialization and assignment? Initialization gives a variable an initial value at the point when it is created. Assignment gives a variable a value at some point after the variable is created.
An initializer is an optional part of a data declaration that specifies an initial value of a data object. The initializers that are legal for a particular declaration depend on the type and storage class of the object to be initialized.
The most common benefit of doing this is improved performance. If the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object.
Short answer: Nothing. The ()
can be used if you want to pass in some constructor args but
in your case since you don't have any, you can skip ()
.
For eg. ()
is useful here.
Foo foo = new Foo(someBar){Prop1 = "value1", Prop2 = value2};
but if you are trying to call the parameter-less constructor, it's optional
Foo foo = new Foo {Prop1 = "value1", Prop2 = value2};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With