0

Possible Duplicate:
What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)

I've recently started compiling a small library of all the extension methods that I've used in my projects over the last 12 months.

Almost all of them are mundane methods that simply provide shortcuts for my everyday programming tasks. e.g. parsing Integers from String literals.

public static int ToInt(this String s) {
    int i;
    int.TryParse(s,out i);
    return i;
}

or taking a String array and converting it back to a String for printing to Console when I'm debugging code.

public static overrride String ToString(this String[] s) {
    StringBuilder sb = new StringBuilder();
    foreach(String item in s) {
        sb.AppendLine(item);
    }
    return sb.ToString();
}

I really love these little methods... They have saved me endless amounts of typing over the last 12 months.

So my question for everyone else out there: What are your favorite extension methods that you have written, or use on a regular basis, and for what reason?

Thanks.

Community
  • 1
  • 1
Jeremy Cade
  • 1,341
  • 2
  • 17
  • 28
  • string.Join? ..... http://msdn.microsoft.com/en-us/library/tk0xe5h0.aspx – Mitch Wheat Dec 24 '10 at 07:34
  • This cannot be answered objectively. Fortunately, that's why we have http://programmers.stackexchange.com. – Kirk Woll Dec 24 '10 at 07:37
  • @Paul, welcome back to SO. ;) Community wikis for questions have not been voluntary for some time. – Kirk Woll Dec 24 '10 at 07:39
  • Reading your second method I was just wondering if it wouldn't be more efficient to simply Join the string Array using String.Join(Environment.Newline, s); Haven't measured this or anything, was just curious. – Anton Dec 24 '10 at 07:40
  • I'm not sure.. I've never really run the ToString(this String[] s) method against arrays large enough for it to make that much of a difference. I'll try it later against some large datasets and see what happens.. – Jeremy Cade Dec 24 '10 at 07:45
  • Duplicate http://stackoverflow.com/questions/271398/post-your-extension-goodies-for-c-net-codeplex-com-extensionoverflow – Ilya Smagin Dec 24 '10 at 07:42
  • First method: Seriously? You just wrapped TryParse inside another method which does exactly the same thing! Why not use TryParse directly? – bobbyalex May 03 '13 at 01:08
  • @BobbyAlexander syntactic sugar.. – Jeremy Cade May 03 '13 at 01:42

0 Answers0