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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var