Monday, October 1, 2007

Extension Methods in C# 3.0

This is actually "old stuff" today - but it is really cool: extension methods in C# 3.0 and mixins!

Ever wanted to raise a double to the power of another double without having to go like:
Math.Pow(10.343, 302.34);

Well, what do you say for that:

That would be very nice, and... it is possible, as long as:

  • you are using C# 3.0

  • you have implemented an extension method like this:

  •     public static class Powerade
    public static double RaiseToPower(this double x, double y)
    return Math.Pow(x, y);

    This is of course a quite useless example, but what extension methods are in essence is a way to implement mixins in C# 3.0: you can "mix in" some functionality to a type that is already there.

    More on mixins soon.

    No comments: