Welcome to CodeSpread!

Why Anonymous Type?

Anonymous types provide you the ease of having set of properties (Read only) into a single object without knowing the type. It’s the job of compiler to generate the type information.

Anonymous types can be created by using the keyword new. Say:

var test = new { Name = “Jackie”, ID = 123 };

Here ‘var’ keyword plays an important role as the type information won’t be available.

Anonymous types are mostly used with the query expressions. This type can contain one or more Pubic read only properties.

The most common scenario where Anonymous types are useful is shown below:

var details = from data in dt.AsEnumerable()

select new


UID = data.Field("id"),

FirstName = data.Field("fName"),

LastName = data.Field("lName”),

Mobileno = data.Field("mno"),

Address = data.Field<string>("add")


Here, the DataTable dt already has some members or properties say. Now let’s say you don’t need to have all the properties or members of dt.

Then in that case we will make use of Anonymous type using the new keyword. This causes smaller amount of data to be dealt with.

Here we have specified the member names to the upcoming Anonymous type (UID, FirstName etc..)which is not necessary. If at all, we are not passing any names to the members, compiler will use the name of the members of the existing type(id, fname, lname etc..).

As this article is based on developer’s experience so Please provide more inputs to improve this article.

Author: jackie

I am a developer.

Share This Post On


  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • C#: Useful JSON in .NetC#: Useful JSON in .NetWhy JSON? All web applications revolve around large chunk of data and availability of that data to our application is of utmost importance. To do that, we have options like fetch data from databas...
  • IQueryable: Performance boost over remote Data AccessIQueryable: Performance boost over remote Data AccessIf we also follow the inheritance principle like OOPS programming, we need to extend our previous discussion “IEnumerable: Power behind Collections”. So this will lead us to write something about I...
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010?How to create a MVC 2 Project in VS2010? 1. Open VS2010. 2. Select File -> New Project. 3. Select Visual C# -> Web -> ASP.NET MVC 2 Empty Web Application. Name it as MyMvcApplicat...
  • Simply LINQ: What,Why and How.Simply LINQ: What,Why and How.What is LINQ? LINQ is Language-Integrated Query (LINQ). It is primarily an extension to the language's capability where it adds query expressions which processes objects and data. It is a one s...
  • 19 Dec: Must Read Codes [1-5]19 Dec: Must Read Codes [1-5]We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn't require a full length descriptive article so combining them into groups of five. CodeRe...
  • Nullable .Net typesNullable .Net typesWe declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but ....
  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...