user1025901
user1025901

Reputation: 1909

How to add multiple Anonymous Type in c#?

Consider this

var source = new{  Id = "1", Name = "Name1"}

It works fine. But if I want to add 1 more property how to do so...

It failed

var source = new{  Id = "1", Name = "Name1"},new{  Id = "1", Name = "Name1"}

what is the correct syntax for this?

Upvotes: 3

Views: 223

Answers (1)

Oded
Oded

Reputation: 499352

See Collection initializers and anonymous types on MSDN:

var source = new [] { new {  Id = "1", Name = "Name1"},
                      new {  Id = "2", Name = "Name2"}};

Upvotes: 7

Related Questions