web 2.0

Utility Extension Methods

I realize that every time I post code, although I'm still a rookie in this sport, I'm putting my neck out there for some serious criticism.  But, that’s the only way to have fun in the dev world.  Ever since the introduction of Extension Methods in C# 3.0, I find myself writing a lot of utility methods.  By making them Extension methods, I make my code a lot easier to read and I just enjoy the syntax of extension methods anyways.  I’m listing a few of mine that I keep handy for many projects over time.

  1. GetLastRange<T>

    This one is very handy because it lets me get a range from the end of a collection object.  I found that calculating the end and the amount of objects left is tedious if you have to get the trailing end of a collection multiple times.

    public static ReadOnlyCollection<T> GetLastRange<T>(this Collection<T> sourceList, Int32 startIndex)
    {
        var thisList = sourceList.ToList().GetRange(startIndex, sourceList.Count - startIndex);
        return thisList.AsReadOnly();
    }

     

  2.  ToStandardBool

    This method may not seem important, but becomes important when you’re dealing with a strange backend.  I found when querying a few data sources that they return boolean values as string.  I’ve seen many different variations of this, most of the time “1” is true and “0'” is false.  But I've also seen a String.Empty or a null value denote false from a web service.  Instead of chewing out the web service provider, i wrote a method that catches that problem for me in most cases.  I’m sure the culture information is unnecessary.

    public static bool ToStandardBool(this string input) 
    { 
        return input.Equals((1).ToString(), StringComparison.OrdinalIgnoreCase);
    }

     

  3. ToTitleCase

    This method just simplifies converting a string to Title Case.  I felt the standard MSDN way of doing it would clutter a method, so I added this extension method to contain all of that unnecessary logic.

    public static String ToTitleCase(this String input)
    {
        return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(input);
    }

     

  4. AsReadOnly

    This is another simple method that I find very useful.  If you use FxCop, you’ll know that Microsoft suggests mainly returning ReadOnlyCollections from publically visible methods.  This just cuts down on the code that needs to be written to return your LINQ queries in the “correct” manner.

    public static ReadOnlyCollection<T> AsReadOnly<T>(this IEnumerable<T> thisList)
    {
        return thisList.ToList().AsReadOnly();
    }

     

This is just a sampling of the methods I use.  I’m curious what neat extension methods everyone else is using… As I posted on Twitter:

sleek

Tags: ,

.NET | C#

Comments

A. Dandy United States, on 12/6/2009 2:13:53 AM Said:

A. Dandy

Do what you can to avoid ToList and ToArray. I usually have overloads that take an IEnumerable and others that take an array or List, etc.

masenkablast United States, on 12/6/2009 2:31:02 AM Said:

masenkablast

That's pretty slick.  It's feels primitive to have to convert to a List type to accomplish anything.  I really like IEnumerable and the lazy computation way of doing things.  It hurts processing time everytime I have to convert an IEnumerable to a List.  The only downside is FxCop requires that public methods return ReadOnlyCollections that are enumerated.

Daniel Millions United States, on 1/10/2010 4:44:35 AM Said:

Daniel Millions

I'm curious what CMS your website uses? It looks really good and I like all the visitor functions that are available. Sorry if this is the wrong place to ask this but I wasn't sure how to contact you - thanks.

masenkablast United States, on 1/11/2010 12:28:21 AM Said:

masenkablast

I use BlogEngine.NET it's version 1.5.0.7

It's very similar to Wordpress, which I recommend highly! http://wordpress.org/

Gareth Republic of the Philippines, on 2/21/2010 7:46:37 AM Said:

Gareth

I love your blog.  Nice to see people so passionate about c#.  Added you to my RSS feed.