Can I define an array or a list from anonymous class?
-
29-09-2019 - |
Question
Can I define an array or a list from anonymous class?
like this:
persons = new ... []
{
new { ID = 1, Name = "Name1"},
new { ID = 2, Name = "Name2"}
}
Solution
Yes, you'll just need to type the persons
variable implicitly and remove the type specifier from the array creation statement.
var persons = new []
{
new { ID = 1, Name = "Name1" },
new { ID = 2, Name = "Name2" }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow