Anonymous types - understanding, programming and basic rules


Anonymous types

Understanding Anonymous Types

Anonymous types defined with var are not VB variants. The var keyword signals the compiler to emit a strong type based on the value of the operator on the right side. Anonymous types can be used to initialize simple types like integers and strings but detract modestly from clarity and add little value. Where var adds punch is by initializing composite types on the fly, such as those returned from LINQ queries. When such an anonymous type is defined, the compiler emits an immutable—read-only properties—class referred to as a projection.

Anonymous types support IntelliSense, but the class should not be referred to in code, just the members.

The following list includes some basic rules for using anonymous types:
  • Anonymous types must always have an initial assignment and it can’t be null because the type is inferred and fixed to the initializer.
  • Anonymous types can be used with simple or complex types but add little value to simple type definitions.
  • Composite anonymous types require member declarators; for example, var joe = new {Name=”Joe” [, declaratory=value, ...]}. (In the example, Name is the member declaratory.)
  • Anonymous types support IntelliSense.
  • Anonymous types cannot be used for a class field.
  • Anonymous types can be used as initializers in for loops.
  • The new keyword can be used and has to be used for array initializers.
  • Anonymous types can be used with arrays.
  • Anonymous types are all derived from the Object type.
  • Anonymous types can be returned from methods but must be cast to object, which defeats the purpose of strong typing.

Anonymous types can be initialized to include methods, but these might only be of interest to linguists.

The single greatest value and the necessity of anonymous types is they support creating single-use elements and composite types returned by LINQ queries without the need for the programmer to fully define these types in static code. That is, the designers can focus significantly on primary domain types, and the programmers can still create single-use anonymous types ad hoc, letting the compiler write the class definition. Finally, because anonymous types are immutable—think no property setters—two separately defined anonymous types with the same field values are considered equal.

Click here to read the complete chapter

  Book Excerpt: LINQ Unleashed for C#

Reproduced from the book LINQ Unleashed for C#. Copyrightã 2008, Pearson Education, Inc., 800 East 96th Street, Indianapolis, IN 46240.

Click here to read the complete chapter with following topics:
  • Programming with Anonymous Types

  • Databinding Anonymous Types

  • Testing Anonymous Type Equality

  • Using Anonymous Types with LINQ Queries

  • Introducing Generic Anonymous Methods

Write your comment - Share Knowledge and Experience

More Related Links

Essential LINQ

Essential LINQ is the first LINQ book written by leading members of Microsoft's LINQ and C# teams. Writing for architects, developers, and development managers, these Microsoft insiders share their intimate understanding of LINQ, revealing new patterns and best practices for getting the most out of it. Calvert and Kulkarni begin by clearly explaining how LINQ resolves the long-time "impedance mismatch" between object-oriented code and relational databases. Next, they show how LINQ integrates querying into C# as a "first-class citizen." Using realistic code examples, they show how LINQ provides a strongly typed, IntelliSense-aware technology for working with data from any source, including SQL databases, XML files, and generic data structures.

.Net Framework 

This includes introduction of .Net framework, .Net framework architecture, role of assembly and GAC.

.NET Code Security

This includes explanation of code security, Principal object, declarative and imperative security, role-based security, code access security and code group.

.NET Assembly 

This article explains .Net assembly, private and shared assembly, satellite assemblies, resource-only assembly, ResourceManager class, strong name, global assembly cache.

Understanding AJAX

AJAX is an acronym for Asynchronous JavaScript and XML. If you think it doesn't say much, we agree. Simply put, AJAX can be read "empowered JavaScript", because it essentially offers a technique for client-side JavaScript to make background server calls and retrieve additional data as needed, updating certain portions of the page without causing full page reloads.............

AJAX Benefits

AJAX offers benefits to both end users and developers. For end users, it reduces the “rich or reach” conflict; for developers, it helps in overcoming the constraints raised by HTTP.............


ASP.NET AJAX is the name of Microsoft’s AJAX solution, and it refers to a set of client and server technologies that focus on improving web development with Visual Studio. Other companies have their own AJAX solution, often taking a radically different approach..........

AJAX Libraries

In addition to ASP.NET AJAX, many third-party AJAX libraries are available that can be used with ASP.NET, although not all of them were specifically designed for it. Some are mostly focused on providing JavaScript libraries for use from within the browser to make manipulation of the browser DOM (Document Object Model) easier..................


Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring