Extending the Idea of Extension Methods
I've had my fair share of disappointments when I found that the closest common denominator between types I wanted to use was object, leading me to write code in a level I did not like, like:
class A; // A and B are both imported classes with no
class B; // common denominator except for object.
class MyListClass
{
List<object> list = new List<object>();
public void Add(object o)
{
this.list.Add(o);
}
}
The above means that I have waved my compile-time (and if it stays like that, also my run-time) type safety in favor of being generic. There are solutions for this, but how about we take the new extension method syntax and play with it a bit? Now that you can add new features to types using extension methods, why not extend entire types by implementing interfaces?
this class MyClass : IFitsTheList
{
// Extension methods here, if you like...
}
This way, we could revise MyListClass to:
class MyListClass
{
List<IFitsTheList> list = new List<IFitsTheList>();
public void Add(IFitsTheList o)
{
this.list.Add(o);
}
}
Taking a complete type hierarchy and adding an Implemetation by Extension to the base class could prove to be a powerful tool.