Anonymous types

            1. What are the anonymous types?

            2. What is the projection initializer?

            3. What does actually happen when we create an anonymous type?

            4. What is the ways to initialize lists or arrays?

Example 1:

    public class Test

    {

        public int Property1 { set; get; }

        public string Property2 { set; get; }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            Test t1 = new Test{Property1 = 0, Property2 = "0"};

            var t2 = new Test { Property1 = 1, Property2 = "1" };

            var t3 = new { Property1 = 2, Property2 = "2"};

            var t4 = new { Property1 = 3, Property2 = "3" };

            var t5 = new { Property2 = "4", Property1 = 4 };

 

 

            Trace.WriteLine(String.Format("t1 is {0}", t1.GetType()));

            Trace.WriteLine(String.Format("t2 is {0}", t2.GetType()));

 

            Trace.WriteLine(String.Format("t3 is {0}", t3.GetType()));

            Trace.WriteLine(String.Format("t4 is {0}", t4.GetType()));

            Trace.WriteLine(String.Format("t5 is {0}", t5.GetType()));

 

            List<int> l1 = new List<int>(){0, 1, 2, 3, 4};

            var l2 = new[]{ new Test{ Property1 = 0, Property2 = "0"}, new Test{Property1 = 1, Property2 ="1"}};

            Trace.WriteLine(String.Format("l2 is {0}", l2.GetType()));

            //var l3 = new { new {Property1 = 0, Property2 = "0"}, new {Property1 = 1, Property2 = "1"}};

            var l3 = new[] {new {Property1 = 0, Property2 = "0"}, new {Property1 = 1, Property2 = "1"}};

            Trace.WriteLine(String.Format("l3 is {0}", l3.GetType()));

        }

    }

According to this line of code var t3 = new { Property1 = 2, Property2 = "2"}; the C# compiler creates a new anonymous type and it projects not just a value of a property but also its name and field of an anonymous type. That’s why it is called projection initializer. If we change the order of properties in an anonymous type the C# compiler will project a completely new type.

.class private auto ansi sealed beforefieldinit <>f__AnonymousType0<<Property1>j__TPar, <Property2>j__TPar>
    extends [mscorlib]System.Object
{
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
    .custom instance void [mscorlib]System.Diagnostics.DebuggerDisplayAttribute::.ctor(string) = { string('\{ Property1 = {Property1}, Property2 = {Property2} }') Type=string('<Anonymous Type>') }
    .method public hidebysig specialname rtspecialname instance void .ctor(!<Property1>j__TPar Property1, !<Property2>j__TPar Property2) cil managed
    {
        .custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor()
    }
 
    .method public hidebysig virtual instance bool Equals(object 'value') cil managed
    {
        .custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor()
    }
 
    .method public hidebysig virtual instance int32 GetHashCode() cil managed
    {
        .custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor()
    }
 
    .method public hidebysig virtual instance string ToString() cil managed
    {
        .custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor()
    }
 
 
    .property instance !<Property1>j__TPar Property1
    {
        .get instance !<Property1>j__TPar <>f__AnonymousType0`2::get_Property1()
    }
 
    .property instance !<Property2>j__TPar Property2
    {
        .get instance !<Property2>j__TPar <>f__AnonymousType0`2::get_Property2()
    }
 
 
    .field private initonly !<Property1>j__TPar <Property1>i__Field
    {
        .custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = { int32(0) }
    }
 
    .field private initonly !<Property2>j__TPar <Property2>i__Field
    {
        .custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = { int32(0) }
    }
 

}

We can also declare lists or arrays which are filled out by anonymous types but according to this line of code and the error which the C# compiler generates the order of properties is still very important.

var l3 = new[] { new { Property1 = 0, Property2 = "0" }, new { Property2 = "1", Property1 = 1 } };

C# compiler generates:

No best type found for implicitly-typed array              

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 )

Connecting to %s