EnjoySharePoint YouTube Channel

Anonymous Types in C#.Net

In this post we will discuss about Anonymous types in C#.Net. Anonymous types are newly introduced in C#.Net 4.0.

Get Office 365 Enterprise E3 Or Business Premium Subscription & Try out all the features

According to MSDN: 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.

Anonymous types are class types that derive directly from object, and that cannot be cast to any type except object.

We can create anonymous types by using the new operator together with an object initializer like below:


var people = new { ID = 1, Name = “AspDotNetHelp” };

Now you can access like below:

Console.WriteLine(people.ID + people.Name);

Anonymous types contain one or more public read-only properties. No other kinds of class members, such as methods or events, are valid. The expression that is used to initialize a property cannot be null, an anonymous function, or a pointer type.

You can also check out some posts on Asp.Net page life cycle events, Simple JavaScript textbox validation example in Asp.Net and Working with enterprise library for data access in asp.net Part-3

Check out Best Alternative to InfoPath -> Try Now
You May Also like the Following SharePoint Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →