Please see my other C# articles.
Simple, elegant data storage.
One of the latest improvements to the .NET Framework in 4.0 is the ability to use object initializers to create simple data-storage objects without having to create a class or struct. Also, these anonymously-typed objects are created by the compiler so you still have strong-typing on your data.
Once the properties have been defined in the initializer, you get/set them simply using the dot-notation.
Anonymous Types provide “type inference” – using the var keyword to define a local variable without explicitly specifying a variable type.
In this example, I will begin by defining a generic (no pun intended) Anonymous Type to store three test values for first and last name, and the phone number.
Next, I’ll define an array of Anonymous Types and store the contents in a StringBuilder object for writing back to the View.
In order to display the contents of Anonymous Types from the homepage, I’ll use the Html.ActionLink() method to call my AnonymousTypes controller’s Index action method.
By clicking the homepage “Anonymous Types” link, I see the results of my work.