Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between define dictionary

Tags:

c#

.net

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.

like image 208
Nilesh Moradiya Avatar asked Jul 30 '13 06:07

Nilesh Moradiya


2 Answers

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.

like image 60
JaredPar Avatar answered Oct 06 '22 00:10

JaredPar


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.

like image 22
p.s.w.g Avatar answered Oct 06 '22 01:10

p.s.w.g