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.

Create an anonymous type with the new keyword and an object initializer. Inside the braces, define properties as in the 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:



Table Of Contents