c# - Initializer syntax: new ViewDataDictionary { { "Name", "Value" } } -


i searching way pass viewdatadictionary partial view in asp.net mvc came syntax:

new viewdatadictionary { { "name", "value" } } 

i'm bit confused initializer syntax here. can explain me?

viewdatadictionary implements idictionary<string, object>.

idictionary<string, object> collection of keyvaluepair<string, object>.

your viewdatadictionary initializer (outer curly braces) contains set of curly braces represents keyvaluepair<string, object> initializer.

the reason possible explained in this answer.

you can add multiple items comma separating keyvaluepair<string, object> initializers:

var data = new viewdatadictionary { { "name", "value" }, { "name2", "value2" } }; 

same as:

var data = new viewdatadictionary           {               new keyvaluepair<string, object>("name", "value"),               new keyvaluepair<string, object>("name2", "value2")           }; 

essentially, inner curly braces nice syntax initializing keyvaluepair<string, object> objects.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -