views:

212

answers:

3

Possible Duplicate:
Post your extension goodies for C# .Net (codeplex.com/extensionoverflow)

I'm fond of C# 3.0. One of my favorite parts is extension methods.

I like to think of extension methods as utility functions that can apply to a broad base of classes. I am being warned that this question is subjective and likely to be closed, but I think it's a good question, because we all have "boilerplate" code to do something relatively static like "escape string for XML" - but I have yet to find a place to collect these.

I'm especially interested in common functions that perform logging/debugging/profiling, string manipulation, and database access. Is there some library of these types of extension methods out there somewhere?

Edit: moved my code examples to an answer. (Thanks Joel for cleaning up the code!)

+5  A: 

You might like MiscUtil.

Also, a lot of people like this one:

public static bool IsNullOrEmpty(this string s)
{
    return s == null || s.Length == 0;
}

but since 9 times out of 10 or more I'm checking that it's not null or empty, I personally use this:

public static bool HasValue(this string s)
{
    return s != null && s.Length > 0;
}

Finally, one I picked up just recently:

public static bool IsDefault<T>(this T val)
{
    return EqualityComparer<T>.Default.Equals(val, default(T));
}

Works to check both value types like DateTime, bool, or integer for their default values, or reference types like string for null. It even works on object, which is kind of eerie.

Joel Coehoorn
Not that it really matters, but why wouldn't you just "return string.IsNullOrEmpty(s)"?
Max Schmeling
You could do that, too. For some reason I like it this way, but I've seen both.
Joel Coehoorn
And actually, I personally use something a little different, since 9 times out of 10 I'm checking that it's NOT null or empty (has a value), mine looks more like: public static bool HasValue(this string s) { return s != null }
Joel Coehoorn
if you want the syntax strValue.IsNullOrEmpty() , wouldn't you just write it as:public static bool IsNullOrEmpty(this string s){ return string.IsNullOrEmpty(s);}
Jon Erickson
Sigh: see my response to Max's comment.
Joel Coehoorn
I do like this extension method though to add a bit of syntactical sugar.
Jon Erickson
hehe not trying to get on your case Joel.... I haven't seen this extension method before though and I like it +1 =)
Jon Erickson
+2  A: 

Here's a couple of mine:

// returns the number of milliseconds since Jan 1, 1970 (useful for converting C# dates to JS dates)
public static double UnixTicks(this DateTime dt)
{
    DateTime d1 = new DateTime(1970, 1, 1);
    DateTime d2 = dt.ToUniversalTime();
    TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
    return ts.TotalMilliseconds;
}

and a ToDelimitedString function:

// apply this extension to any generic IEnumerable object.
public static string ToDelimitedString<T>(this IEnumerable<T> source, string delimiter, Func<T, string> action)
{
    if (source == null)
    {
        throw new ArgumentException("Source can not be null.");
    }

    if (delimiter == null)
    {
        throw new ArgumentException("Delimiter can not be null.");
    }

    string strAction = string.Empty;
    string delim = string.Empty;
    var sb = new StringBuilder();

    foreach (var item in source)
    {
        strAction = action.Invoke(item);

        sb.Append(delim);
        sb.Append(strAction);
        delim = delimiter;
    }
    return sb.ToString();
}
Jeff Meatball Yang
Note that my clean up doesn't exactly match your behavior: you didn't append anything if strAction was empty, this will still put an empty item there.
Joel Coehoorn
String.Join is more appropriate in this case - I'll post an example here shortly.
Erik Forbes
Example posted. I prefer using library methods when available, and I figured I'd show an example.
Erik Forbes
thanks guys. I see my question is an exact duplicate, and there's even a codeplex project extensionoverflow! Awesome!
Jeff Meatball Yang
+1  A: 

Here's Jeff's ToDelimitedString written using String.Join:

public static string ToDelimitedString<T>(this IEnumerable<T> source, string delimiter, Func<T, string> action) {
 // guard clauses for arguments omitted for brevity

 return String.Join(delimiter, source.Select(action).ToArray());
}
Erik Forbes