views:

554

answers:

5

Is there any way I can add a static extension method to a class.

specifically I want to overload Boolean.Parse to allow an int argument.

A: 

It doesn't look like you can. See here for a discussion on it

I would very much like to be proven wrong though.

Ray
+11  A: 

In short, no, you cant.

Long answer, extension methods is just syntactic sugar. IE:

if you have an extension method on string let's say:

public static string SomeStringExtension(this string s)
{
   //whatever..
}

When you then call it:

myString.SomeStringExtension();

the compiler just turns it into:

ExtensionClass.SomeStringExtension(myString);

So as you can see, there's no way to do that for static methods.

And another thing just dawned on me: what would really be the point of being able to add static methods on existing classes? You can just have your own helper class that does the same thing, so what's really the benfit in being able to do:

Bool.Parse(..)

vs.

Helper.ParseBool(..);

Doesn't really bring much to the table...

BFree
+1 for the "what's the point" argument
lc
...and although overloading Boolean.Parse would be cute, you may as well just make a ToBoolean extension method on Int32.
lc
Then what's the point of extension methods at all?
Josh Einstein
Well with extension methods, it sort of gives the illusion that method is actually part of the class, so you can just do myObj.MyExtension() which is much shorter and cleaner than Extensions.MyExtension(obj); For static methods though, you have to use some class name, and then the method either way. So what's the difference if it's Bool.Parse or Helper.Parse, doesn't change anything...
BFree
Static extensions would help with discoverability. For example, in order to call the new overload of "parse" a developer would have to know, ahead of time, that it was defined in "Helper" and go look there. If, on the other hand, it was possible to define a static extension method then someone could discover the new overload by typing "Bool." and seeing it in the completion list.
Scott Wisniewski
@whats the pointA data bound control i am using calls Boolean.parse in a method, if the arg is not boolean. And My database column means false if value<0 and true otherwise.And no I cant use a boolean column here. as numbers other than 0,1 are meaningful too, but dont make a difference in parsing to boolean
Midhat
Then in that case even if you could make a static extension method it wouldn't help because anything binding to Boolean.Parse (either statically bound or late bound via reflection) would never see your extension method anyway. They are simply a C# compiler illusion.
Josh Einstein
A: 

No, but you could have something like:

bool b;
b = b.YourExtensionMethod();
BobbyShaftoe
+2  A: 

specifically I want to overload Boolean.Parse to allow an int argument.

Would an extension for int work?

public static bool ToBoolean(this int source){
    //do it
    //return it
}

Then you can call it like this:

int x = 1;

bool y=x.ToBoolean();
pyrochild
+2  A: 

You could add an extension method to int

public static class IntExtensions
{
 public static bool Parse(this int value)
 {
  if (value == 0)
  {
   return true;
  }
  else
  {
   return false;
  }
 }
 public static bool? Parse2(this int value)
 {
  if (value == 0)
  {
   return true;
  }
  if (value == 1)
  {
   return false;
  }
  return null;
 }
}

used like this

  bool bool1 = 0.Parse();
  bool bool2 = 1.Parse();

  bool? bool3 = 0.Parse2();
  bool? bool4 = 1.Parse2();
  bool? bool5 = 3.Parse2();
Simon