i am developing application using C#(5.0).Net(4.5). I want to know whats the difference between below to declaration of dictionary variable. 1.
var emailtemplateValues = new Dictionary<string, object>
{
{"CreatorName", creatorUserInfo.UserName},
{"ProjectName", masterProjectInfo.Title},
{"Reason", reason}
};
and 2.
var emailtemplateValues = new Dictionary<string, object>()
{
{"CreatorName", creatorUserInfo.UserName},
{"ProjectName", masterProjectInfo.Title},
{"Reason", reason}
};
in 2nd declaration i have used ()
after Dictionary<string, object>
.
both syntax works fine but just eager to know about internal work.
These two syntaxes are equivalent. When a constructor call is omitted from the initializer expression then the compiler will attempt to bind to the parameterless constructor an that type. This is covered in section 7.5.10.1 of the C# spec
An object creation expression can omit the constructor argument list and enclosing parentheses provided it includes an object initializer or collection initializer. Omitting the constructor argument list and enclosing parentheses is equivalent to specifying an empty argument list.
Nothing. When using the initializer syntax, you may omit the the parentheses. This is equivalent to invoking the parameterless constructor. They will produce exactly the same byte-code when compiled.
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