Prash's Blog

Using Anonymous types in .Net 3.0 September 28, 2009

Filed under: C# — prazjain @ 2:20 am
Tags: ,

Using Anonymous types in C# .Net 3.0

Anonymous types should as simple containers for storing temporary data. These types as they do not have a name associated with them, cannot be passed to a method or returned back. Generally these are supposed to be used inside a method while performing some processing, where you would not like to work on the entire data structure and just a part of it.

How to create an anonymous type

<p>var anonTypeVar = new { X = 100, Y = 200 };</p>

This statement causes compiler to generate an internal sealed class, with readonly properties for X and Y. The constructor for this class takes two parameters for each property, in the sequence they are defined. The type of the properties is determined by their values.
eg. : This is approximately what compiler generates behind the scenes.

<p>internal sealed AnonTypeClass {<br />
private readonly int x;<br />
public int X { get { return x; } }</p>
<p>private readonly int y;<br />
public int Y { get { return y; } }</p>
<p>public AnonTypeClass(int xParam, int yParam) { x = xParam ; y = yParam; }</p>

The compiler generates this boiler plate code for you when you create an anonymous type var.

In case you noticed by now, using object initializers with immutable types is only possible when its an anonymous type, as compiler adds some features into the auto-gen code to allow that.

You might be tempted to think that the compiler create a new type everytime it sees that expression. No, it reuses the same type as long as their are same number of properties, they are of same type, same name, and in the same sequence.
So these expressions would generate different types :

<p>var a1 = new { X = &quot;a&quot;, Y = &quot;b&quot; };<br />
var a2 = new { Y = 100, X = 200 };<br />
var a3 = new { X = 100, Y = 200, Z = 300 };</p>

Overuse of anonymous types could make the code unreadable, but if used judiciously they could prevent creation of lots of classes that are used to create only temporary objects  in intermediate processing.


Extension Methods : What When How

Filed under: C# — prazjain @ 1:24 am
Tags: ,

What are Extension Methods

Interfaces have allowed us to attach only declaration of methods with them.
Extension methods provide a way to add behavior to an interface. This makes it more interesting as your interface definition and the implementing class can remain constant, but the new capabilities can be added to the interface (through extension methods).

When to use Extension methods

A general rule of thumb that goes while determining which methods should be part of interface and which ones should be extension methods :

Any methods that is part of generic implementation should be part of interface, whereas any specific implementation of that method should be an extension method.

Suppose there is a holiday planning service out there, then using extension how could it be designed :

public interface IHolidayPlanner
IList<Holiday> GetHolidays(int minPrice, int maxPrice, int duration);

public class HolidayPlanner : IHolidayPlanner
IList<Holiday> GetHolidays(int minPrice, int maxPrice, int duration)
return null // get the logic working here

// Now we could define extension methods around the generic implementation of IHolidayPlanner interface, for example searching for budget holidays and luxury holidays

public static class HolidayPlannerEx
public static IList<Holiday> GetBudgetHolidays<T>(this T planner,int duration) where T: IHolidayPlanner<T>
return planner.GetHolidays(0,1000,duration);

public static IList<Holiday> GetLuxuryHolidays<T>(this T planner,int duration) where T: IHolidayPlanner<T>
return planner.GetHolidays(1001,100000,duration);

In this way any specific implementation that could be built upon a generic implementation of a method in the interface could be declared as an extension method.

This example might not be the best way to design a holiday planning app, but I hope it get the message across about how Extension methods can be used.