Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Wednesday, January 23, 2008

Yesterday I wrote an article explaning what var means in .NET 3.x. Today lets talk about Anonymous Types.

Anonymous Types as describe by the C# 3.0 specification are tuple types automatically inferred and created from object initializers. Anonymous Types allows the new operator to be used with an anonymous object initializer to create an object at compile time. The format for an anonymous type declaration is a follows

var v = new { p1 = e1 , p2 = e2 , ... px = ex }

where v is the var variable, px denotes the property name and ex is equaivalent value.

Enough with the theory and lets look at some actual code. Lets assume that I have a class called Person with the following properties:

public class Person
{
    private string _name;
    private int _age;
    private bool _believesInJesus;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }

    public bool BelievesInJesus
    {
        get { return _believesInJesus; }
        set { _believesInJesus = value; }
    }
}

Let's say that I want to create an Anonymous Type that has a similar structure from what we have above, all I need to do is make this call:

var human = new { Name = "Keith", Age = 25, BelievesInJesus = true };

What happens in compile time is that the .NET compiler generates a class that represents the structure of your Anonymous Type.

public class __Anonymous1
{
    private string _name;
    private int _age;
    private bool _believesInJesus;

    public string Name { get { return _name; } set { _name = value; } }
    public int Age { get { return _age; } set { _age = value; } }
    public bool BelievesInJesus { get { return _believesInJesus; } set { _believesInJesus = value; } }
}

It's an  "Anonymous Type" because it a nameless class, it's generated by the compiler for you and it directly inherits from object.

To prove this let's look at this example:

class Program
{
    static void Main(string[] args)
    {
        var person = new { Name = "Keith", Age = 25, BelievesInJesus = true };

        Console.WriteLine(person.Name);
        Console.WriteLine(person.GetType());
        Console.ReadLine();
    }
}

Hovering at the person variable would give us some information about it's type:

The code above will have this output:

As you can see, the compiler created an Anonymous Type Called <>f__AnonymousType0`3. One thing to note about Anonymous Types is that the compiler is smart enough to figure out if an Anonynous Type has already been declared that meets the schema requirement of your new Anonymous Type.

    class Program
    {
        static void Main(string[] args)
        {
            var person1 = new { Name = "Keith", Age = 25, BelievesInJesus = true };
            var person2 = new { Name = "Charissa", Age = 23, BelievesInJesus = true };
            var person3 = new { Name = "Peter", Age = 23};

            Console.WriteLine(person1.Name);
            Console.WriteLine(person1.GetType());
            Console.WriteLine(person2.Name);
            Console.WriteLine(person2.GetType());
            Console.WriteLine(person3.Name);
            Console.WriteLine(person3.GetType());
            Console.ReadLine();
        }
    }

The result would be:

I hope I was able to show you what Anonymous Types are in C#. Next up is Extension Methods.

Wednesday, January 23, 2008 7:58:09 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Fun Stuff | Tutorial
Archive
<January 2008>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 246
This Year: 43
This Month: 4
This Week: 0
Comments: 111
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)