Using Extension Methods

I came up with the following extensions method in C#:

public static class NeatExtensions{
    public static bool In<T>(this T element, params T[] elements){
        return new HashSet<T>(elements).Contains(element);
    }
}

It actually yields a nice syntax for checking enum values and the likes:

enumValue.In(Enum.X, Enum.Z)

I am wondering whether there is actually a de-factor standard library in the C# world that has extensions like this.


Posted

in

by

Tags:

Comments

2 responses to “Using Extension Methods”

  1. Sarah Taraporewalla Avatar

    Hey Felix,
    Not that I have seen although I was going to start one (similar to the facets project in ruby) where you have all the nice little extension methods for system objects, such as string, IEnumerable etc.

    Every project I am on I seem to be bringing the same “xxxExtensionMethods.cs” file with me!
    The dot.net bites talk that Dan Xian and I did at away did listed a few more fun extension methods you can use (like list.AsString())

  2. Mark Needham Avatar

    I haven’t used either of these libraries but I think they have put together some of the typical extension methods:

    http://www.codeplex.com/nxl
    http://www.codeplex.com/umbrella

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.