Reputation: 721
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.
Upvotes: 5
Views: 100
Reputation: 172
There are no difference after the processed by compiler the output is same code.
Upvotes: 1
Reputation: 754763
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.
Upvotes: 9
Reputation: 7629
No difference here, the first one is simply a syntactic sugar for the second.
Upvotes: 5
Reputation: 149020
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.
Upvotes: 5