Anonymous types

suggest change

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

You can make anonymous types by using the new keyword followed by a curly brace (\{). Inside the curly braces, you could define properties like on code below.

var v = new { Amount = 108, Message = "Hello" };

It’s also possible to create an array of anonymous types. See code below:

var a = new[] { 
    new { 
        Fruit = "Apple", 
        Color = "Red" 
    },
    new {
        Fruit = "Banana",
        Color = "Yellow"
    }
};

Or use it with LINQ queries:

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


C# 3.0 Features:
* Anonymous types

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
73 C# 3.0 Features
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script