Click here to Skip to main content
13,345,744 members (50,247 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 18 Sep 2013

Compiler and Anonymous Type

, 18 Sep 2013
Rate this:
Please Sign up or sign in to vote.
How compiler manage anonymous types


In this tip, I will explain how anonymous types work with MSIL, and how the framework manages them and anonymous types.

What is an Anonymous Type?

Anonymous type means a user will define properties of a type and the compiler will create that type with a compiler generated name and the type details at compile time.

If you want some regular definition, then you can check it out on MSDN.

var AnoClass = new { Name = "Suvabrata", Age = 25,Sex='M' }; 

The above statement will create a class which has three properties and that class would be named at compile time like <>f__AnonymousType0`3.

  • Now why do compilers choose this name?
  • How will the compiler create or build those types?
  • What is the advantage of these types?

These classes are named in a pattern that starts with <>f__AnonoymousType, now 0 is the index or sequence of that class and 3 is the property count of that class.

Features of Anonymous Class

  1. All properties are read only property.
  2. Compiler will create different classes for anonymous type depending on property name (Case sensitive), property sequence and property count. It does not depend on Property type (as they are generic types created by compiler).
  3. You cannot declare a field, a property, an event, return type of a method, formal parameter of a method, property, constructor, or indexer as having an anonymous type.
  4. Don't try to pass anonymous types outside of method boundary. If you pass anonymous type outside of method boundary, it will defeat the purpose of strong types.

Anonymous type is basically a generic type (as compiler creates) what the user will name each property the compiler will create a generic property at compile time by taking the right hand side's type.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Suvabrata Roy
Architect ICRA Online Ltd.
India India
He has a experience of 7+ years in .Net Technology, he like to learn and share thinks about coding and development process.

Quora | LinkedIn | Twitter

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
patel_pratik18-Sep-13 20:38
memberpatel_pratik18-Sep-13 20:38 
GeneralRe: My vote of 5 Pin
Suvabrata Roy18-Sep-13 20:49
professionalSuvabrata Roy18-Sep-13 20:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 19 Sep 2013
Article Copyright 2013 by Suvabrata Roy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid