ASP.NET 3.x and above: creating dynamic, or anonymous types
A relatively unknown feature in available since .NET 3.0 is the ability to create dynamic or more correctly anonymous types .
These are typically used in LINQ select scenarios
from p in db.Users
select new (p.Name, p.Email}
This basically creates an anonymous type with Name and Email properties.
With all the new advancements in web technologies, there is this increasing need to have the ability to create objects on the fly so as to pass to web api or to send data across different systems.
Creating an anonymous object in code is pretty simple, below is an example of how you create one
var obj = new {ID=1, Name=”Jeffery”, Email=”a@a.com”};
This basically creates a new object on the fly with the following 3 properties
- ID
- Name
- For more reading, look at the following links:
http://msdn.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic15