Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize IDictionary in constructor?

Tags:

c#

asp.net-mvc

In TagBuilder and other classes I can write something like:

var tr = new TagBuilder("HeaderStyle"){InnerHtml = html, [IDictionary Attributes]}

but I don't know how to pass the IDictionary parameter.

How can I do that on the fly? Without creating a Dictionary variable.

TagBuilder is an example, there are other classes that accept a parameter IDictionaryas well. The question is about the generic case.

like image 644
eKek0 Avatar asked Jul 30 '09 00:07

eKek0


1 Answers

Another way to create Dictionaries from Anonymous types:

new Dictionary<int, StudentName>()
{
    { 111, new StudentName {FirstName="Sachin", LastName="Karnik", ID=211}},
    { 112, new StudentName {FirstName="Dina", LastName="Salimzianova", ID=317}},
    { 113, new StudentName {FirstName="Andy", LastName="Ruth", ID=198}}
};

http://msdn.microsoft.com/en-us/library/bb531208.aspx

like image 138
Robert Harvey Avatar answered Nov 15 '22 09:11

Robert Harvey