4

I know I can extend the string class like so:

public static class EMClass
{
   public static int ToInt32Ext(this string s)
   {
      return Int32.Parse(s);
   }
   public static int ToInt32Static(string s)
   {
      return Int32.Parse(s);
   }
}

And use it like this:

string x = "12";
x.ToInt32Ext()

But how can I make it to where I can use it like this:

int x = string.ToInt32("15");
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
Bob Dylan
  • 4,393
  • 9
  • 40
  • 58
  • Remember that the first argument in the method signature, (this T argt), represents the type, and not the instance of the type! – Claus Jørgensen Aug 18 '09 at 18:09
  • 6
    @Claus This is wrong - the first parameter contains the instance the extension method was invoked on (or null if the extension method was invoked on null). – Daniel Brückner Aug 18 '09 at 18:13

6 Answers6

12

I don't think you can do "static" extension methods.

Andy Gaskell
  • 31,495
  • 6
  • 74
  • 83
3

Dude, do you have any problems with Int32.Parse() ?

Leahn Novash
  • 2,861
  • 2
  • 20
  • 18
  • Agree- even if the code was actually possibly, this is an excellent example of a useless extension method. – RichardOD Aug 18 '09 at 18:47
3

What I did in our project at work is created a class called Strings which is where I put all of our extension methods. That way you have something that visually looks similar to string.

You can call the extension method in your example like so too (not all people might like this, but I use this myself depending on the code I'm working on):

int x = "15".ToInt32();
Community
  • 1
  • 1
Brian Surowiec
  • 17,123
  • 8
  • 41
  • 64
1

Extension methods only apply to instances of a class, not the class itself. You can't extend a class with a class method using an extension. Note that you should easily be able to do what you want with an extension method, just using.

var fifteen = "15".ToInt32Ext();
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
1

You can't really do this... the closest you are going to get is this:

public static class Ext
{
    public static class String
    {
        public static int ToInt32(string val)
        {
            return Int32.Parse(val);
        }
    }
}

//Then call:
Ext.String.ToInt32("32");

Which i'm surprised the compiler even allows you to name a class "String"

Just for entertainment, i thought of another fun way to do it, though i would never recommend this. Add:

using String = NameSpace1.Ext.String;

//Then you can access it as:
String.ToInt32("32"); //Yipes
Brian Rudolph
  • 6,142
  • 2
  • 23
  • 19
  • Why a surprise? You can have a lot of class with the same name, just not in the same namespace ;) – Patrick Desjardins Aug 18 '09 at 18:32
  • You *almost* got my +1. I was with you until you've created another String, I find it very confusing. But your answer is correct: if he wants a static method, it's easiest to create a static class - he has to use a class anyway. – Kobi Aug 18 '09 at 18:56
  • Well, he wanted an answer of how he could do it. The simple answer is "no you can't do that", unfortunately thats not entirely true. You could do it, i would just not recommend it at all. Surely, future developers on the proejct would hate you for it. – Brian Rudolph Aug 18 '09 at 19:08
0

Personally, I don't see a lot of use for an extension-method wrapper to a method call that's already a short one-liner. On the other hand, wrapping the Int32.TryParse pattern to return a nullable int can be pretty convenient:

    public static int? ToInt32(this string input)
    {
        if (String.IsNullOrEmpty(input))
            return null;

        int parsed;
        if (Int32.TryParse(input, out parsed))
            return parsed;

        return null;
    }

This allows you to parse a value that might not be present, or might not contain an integer, and provide a default value in a single line of code:

int id = Request.QueryString["id"].ToInt32() ?? 0;
Joel Mueller
  • 28,324
  • 9
  • 63
  • 88