O'Reilly logo

WCF 4.5 Multi-Layer Services Development with Entity Framework by Mike Liu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Anonymous types

With the feature of the object initializer and the var datatype, we can create anonymous datatypes easily in C#.

For example, we can define a variable as follows:

var a = new { Name = "name1", Address = "address1" };

At compile time, the compiler will actually create an anonymous type, as follows:

class __Anonymous1
{
    private string name;
    private string address;
    public string Name {
        get{
            return name;
        }
        set {
            name=value
        }
    }
    public string Address {
        get{
            return address;
        }
        set{
            address=value;
        }
    }
}

The name of the anonymous type is automatically generated by the compiler and cannot be referenced in the program text.

If two variables that are defined by anonymous types have the same members with the same datatypes in their initializers, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required