Home > ASP.NET > ASP.NET 3.x and above: creating dynamic, or anonymous types

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

  1. ID
  2. Name
  3. Email
    For more reading, look at the following links:

http://msdn.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic15

http://msdn.microsoft.com/en-us/library/bb397696.aspx

Categories: ASP.NET
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: