Anonymous type in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 2950 red flag
Rating: 5 out of 5  
 1 vote(s)

How to create Anonymous type in C#

Anonymous type in C#

In this article we will discuss about anonymous type in C#. This concept has introduced in C # 3.0 versions. Basically anonymous concept is used to crate data type at runtime. There is no need to create any user define data type (like class or structure) previously. In the example below we will see how to create anonymous type in C#.


Create simple anonymous type.

To create anonymous type we will use var keyword. As, we don’t know actual type of anonymous variable using new keyword we will initiate few members in anonymous type.  

using System;
using System.Collections;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
 
namespace BridgeDesign
{
    class Program
    {
        static void Main(string[] args)
        {
 
            var MyClass = new { name = "Sourav", surname = "Kayal" };
            Console.WriteLine("Name:- "+ MyClass.name);
            Console.WriteLine("Surname:- " + MyClass.surname);
 
            Console.ReadLine();
        }
    }
}

Here is sample output.

 

How it works in background?

Let’s discuss a little how anonymous type converts in strong type. Here a class without a name is generated with two data member. For anonymous type syntax the compiler generates a class. The class contents those variables which are declared within new block. In compile time compiler make it as strong type class and Visual Studio Intellisense supports anonymous type. Because when we try to access member of anonymous type we can get help from Intellisense like below.

 

 Use one anonymous type within other anonymous type

We can use one anonymous type within other anonymous type like below.

using System;
using System.Collections;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
 
namespace BridgeDesign
{
    class Program
    {
        static void Main(string[] args)
        {
            var MyStudent = new { name = "Sourav", surname = "Kayal" };
            var ClassRoom = new { MyStudent.name, MyStudent.surname, ClassRoom = "MCA" };
 
            Console.WriteLine("Student Name:- "+ ClassRoom.name);
            Console.WriteLine("Student surname:- " + ClassRoom.surname);
            Console.WriteLine("Class Name:- " + ClassRoom.ClassRoom);
            Console.ReadLine();
        }
    }
}
 
Here is sample output

 

Conclusion:

This is basic idea about anonymous type in c#. Hope you have understood the concept.


Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: t5j9033387989 on: 9/12/2013 | Points: 25
Anonymous types are types created on the fly typically in order to return results in a LINQ statement. Here's an example from MSDN

var productQuery = 

from prod in products
select new { prod.Color, prod.Price };


A new type with the read-only properties Color and Price is created and the query returns instances of this type when enumerated.

foreach(var product in productQuery) {

Console.WriteLine(product.Color);
}


product will be of the anonymous type defined above.

Anonymous types are useful for returning several properties from a query without having to define a type explicitly for that purpose.

Login to post response

Comment using Facebook(Author doesn't get notification)