Create Anonymous Type inside an Array in CSharp
Description
The following code shows how to create Anonymous Type inside an Array.
Example
//www.j a v a 2 s.c o m
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
class AnonymousTypeInArray
{
static void Main()
{
var family = new[]
{
new { Name = "H", Age = 31 },
new { Name = "R", Age = 1 },
new { Name = "W", Age = 1 }
};
int totalAge = 0;
foreach (var person in family)
{
totalAge += person.Age;
}
Console.WriteLine("Total age: {0}", totalAge);
}
}
The code above generates the following result.