Anonymous type

In this chapter you will learn:

  1. Anonymous types
  2. Limitations on anonymous types
  3. Initializing Anonymous Type Arrays

var

Declare a variable using the special type var, and then define the type's content using the new keyword.

Anonymous types encapsulate a set of properties into a single object.

The types of the properties in an anonymous type are inferred by the compiler.

You use var to declare variable. C# infers the type of the variable.

using System;// j  a v  a2s.c om

class Program
{
    static void Main(string[] args)
    {
        var i = 5;
        Console.WriteLine(i);
        Console.WriteLine(i.GetType());

        var str = "java2s.com";
        Console.WriteLine(str);
        Console.WriteLine(str.GetType());

        var f = 0.0F;
        Console.WriteLine(f);
        Console.WriteLine(f.GetType());
    }
}

The output:

var type variable is static typed. You cannot change the type for a var-type variable.

using System;/*  j  av a  2 s .c o  m*/

class Program
{
    static void Main(string[] args)
    {
        var i = 5;
        i = "java2s.com";
    }
}

The code above has the following error message:

The following fragment creates an anonymous type with two properties a string and an int.

var my = new { 
    Name = "Your Name", 
    Age = 37; 
};

The values of the properties can be accessed by calling my.Name and my.Age.

Limitations on anonymous types

Anonymous types have some limitations:

  • The properties are read-only, only properties can be defined.
  • Their scope is limited to the method in which they are created.
  • Anonymous types cannot be passed as arguments to other methods.

The following example creates a nested anonymous type:

using System;/* j  a  v a  2  s . c  om*/
public class MainClass
{
    static void Main(string[] args)
    {
        var my = new
        {
            Name = "Jack",
            Age = 35,
            Address = new
            {
                Street = "Main Street",
                City = "New York"
            },
        };
        // Access the members of the anonymous type. 
        Console.WriteLine("Name: {0}", my.Name);
        Console.WriteLine("Age: {0}", my.Age);
        Console.WriteLine("Street: {0}", my.Address.Street);
        Console.WriteLine("City: {0}", my.Address.City);
    }
}

The output:

Initializing Anonymous Type Arrays

using System;/*from   j  av a2s  .c  o  m*/

class Program
{
  static void Main()
  {
          var worldCup2006Finalists = new[]{new
          {
              TeamName = "France",
              Players = new string[]
              {
                  "A", "B","C", "D",
              }
          },
          new
          {
              TeamName = "Italy",
              Players = new string[]
              {
                  "Q", "W","E", "R",
              }
          }
      };
      Print(worldCup2006Finalists);
  }

  private static void Print<T>(IEnumerable<T> items)
  {
      foreach (T item in items)
      {
          Console.WriteLine(item);
      }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. Get to know bool type
  2. String representing the true and false value
  3. Output a boolean variable with Console.WriteLine
Home » C# Tutorial » Data Types
C# Data Types
Value type vs reference type
Literals
Value default value
Anonymous type