Creates an anonymous type that itself contains a nested anonymous type, and then prints out the various fields: - CSharp Custom Type

CSharp examples for Custom Type:Anonymous Types

Description

Creates an anonymous type that itself contains a nested anonymous type, and then prints out the various fields:

Demo Code

using System;/*from ww  w  . j  a va 2 s.c  o m*/
class MainClass
{
   static void Main(string[] args)
   {
      // create an anoymous type
      var joe = new {
         Name = "Mary",
         Age  = 42,
         Family = new {
            Father = "f",
            Mother = "m",
            Brother = "b"
         },
      };
      // access the members of the anonymous type
      Console.WriteLine("Name: {0}", joe.Name);
      Console.WriteLine("Age: {0}", joe.Age);
      Console.WriteLine("Father: {0}", joe.Family.Father);
      Console.WriteLine("Mother: {0}", joe.Family.Mother);
      Console.WriteLine("Brother: {0}", joe.Family.Brother);
   }
}

Result


Related Tutorials