Anonymous Types

Using anonymous type you can create your own objects without defining them.

Let’s understand what does it mean. In below example we have created the collection of company and then extracted the data from it using the linq. Then we have loop through the each object and printed the value on console.

class Company
{
        public string Name { get; set; }
        public string Type { get; set; }
}

var Companies = new List{
        new Company{ Name = "WhiteX", Type ="Learning" },
        new Company {Name = "WhiteX", Type ="Solution"},
};

//Linq
Console.WriteLine("\nUsing Linq.");
var Result = from c in Companies
             select c;

foreach (var item in Result){
         Console.WriteLine("Name: {0} \t Type: {1}", item.Name, item.Type);
}
//End Linq

In the above example, what compiler will do, it will create the object of return data type of LINQ query and assign to Result variable.

In the beginning I have mentioned that “Using anonymous type you can create your own objects without defining them”. It new if you want to create your own object with different property name using the existing object that you can do in Linq query itself by using anonymous type. To create anonymous type you need to use the new keyword in select query along with curly ({}) braces and then specify the property name you want to use for newly created object.

//Anonymous Type
Console.WriteLine("\nUsing Anonymous Type.");
var AnonymousResult = from c in Companies
                      select new { CompanyName = c.Name, CompanyType = c.Type};

foreach (var item in AnonymousResult)
{
        Console.WriteLine("Name: {0} \t Type: {1}", item.CompanyName, item.CompanyType);
}    
//End

Above shown is the sample code for the anonymous type created using company object.

Below is the complete code for above example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnonymousType
{
    class Program
    {
        static void Main(string[] args)
        {
            var Companies = new List{
                        new Company{ Name = "WhiteX", Type ="Learning" },
                        new Company {Name = "WhiteX", Type ="Solution"},
            };

            //Linq
            Console.WriteLine("\nUsing Linq.");
            var Result = from c in Companies
                         select c;

            foreach (var item in Result){
                Console.WriteLine("Name: {0} \t Type: {1}", item.Name, item.Type);
            }
            //End Linq

            
            //Anonymous Type
            Console.WriteLine("\nUsing Anonymous Type.");
            var AnonymousResult = from c in Companies
                                  select new { CompanyName = c.Name, CompanyType = c.Type};

            foreach (var item in AnonymousResult)
            {
                Console.WriteLine("Name: {0} \t Type: {1}", item.CompanyName, item.CompanyType);
            }    
            //End

        }
    }

    class Company
    {
        public string Name { get; set; }
        public string Type { get; set; }
    }
}

Note*: When you create the anonymous type all properties become read only.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s