Anonymous types in C# 3.0

No.of Views811
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  06 Sep 2010 10:09:14
Tag : CSharp , Miscellaneous
Anonymous types are new feature being added in C# 3.0.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

1. Anonymous types are new feature being added in C# 3.0.
2. These are data types being generated on the fly at the run time.
3. These data types are generated through compiler rather than explicit class definition.

Program.cs

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

namespace ConsoleApplication17
{class Program
    {static void Main(string[] args)
        {

            var MyAnonClass = new{
                Name = "Scott",
                Subject  = "ASP.Net"};

            
            Console.WriteLine(MyAnonClass.Name);
            Console.Read();
        }
    }
}

 Output 

Image Loading

A class without name is being generated and assigned to implicitly type local variable

How it works internally?

1.    For an anonymous type syntax compiler generates a class.
2.    Properties of the complier generated class are value and data type given in declaration.
3.    Intellisense supports anonymous type class properties. 

Image Loading

4.Properties of one anonymous type can be used in declaration of another anonymous type. 

Image Loading

 Program.cs

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

namespace ConsoleApplication17
{class Program
    {static void Main(string[] args)
        {

            var MyAnonClass = new{
                Name = "Scott",
                Subject = "ASP.Net"};


            Console.WriteLine(MyAnonClass.Name);
            Console.Read();

            var SecondAnoynClass = new{
                info = MyAnonClass.Name + MyAnonClass.Subject
            };

            Console.WriteLine(SecondAnoynClass.info);
            Console.Read();
        }
    }
}

 Output

Image Loading

that's all. happy coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top