0

Hallo I would like to create an extension method for Char class that works as Char.IsDigit() method (but that will of course recognize a differnt type of characters). I wrote this:

namespace CharExtensions
{
    public static class CompilerCharExtension
    {
        public static Boolean IsAddOp(this Char c)
        {
            return c.Equals('+') || c.Equals('-');
        }
    }
}

that works fine but that it's not exactly what I meant. This extension should be used this way:

using CharExtensions;
char x:
...
if(x.IsAddOp())
    Console.WriteLine("Add op found");

While I would something like this:

using CharExtensions;
char x;
...
if(Char.IsAddOp(x))
    Console.WriteLine("Add op found");

Thanks to everyone who could help me.

weirdgyn
  • 886
  • 16
  • 47
  • You can't do that and if you could it would be pretty bad practice. Why would you want to do that? Just make a normal method and call it like this: `IsAddOp(x)`. – DavidG Aug 06 '17 at 09:58
  • @DavidG _Just make a normal method_ is good but if could define Extension method its better to use that for developer – Ali Aug 06 '17 at 10:02
  • @combo_ci In what way would an extension to `char` be better? – DavidG Aug 06 '17 at 10:03
  • @DavidG no i say its better to use Extension method rather than normal method, but the question mention to add a function to .Net base class that i think its not possible – Ali Aug 06 '17 at 10:05
  • @combo_ci Actually adding an extension to base framework types is also considered very bad form. – DavidG Aug 06 '17 at 10:06
  • @DavidG just to have a sort o 'uniformity' of calls. Is just an aesthetic matter. – weirdgyn Aug 06 '17 at 10:18

2 Answers2

3

You cannot do that, as extension methods will always require an instance of the object. See here

Extension methods are defined as static methods but are called by using instance method syntax.

Andrei Matracaru
  • 3,511
  • 1
  • 25
  • 29
  • Technically they don't require an instance. They only need a variable of the correct type; whether that one is `null` or not is irrelevant for being able to call the method (although most extensions methods will require it). – Joey Aug 06 '17 at 10:13
  • 1
    @Joey Technically you do not need a variable either. You just need an expression with the correct compile-time type. For example `((IEnumerable)null).First();` – Jeppe Stig Nielsen Aug 06 '17 at 10:21
0

Your question mention to fire a static method of a class. You actually want to define a static method for Char class, not add a extension to char instance. to define a static method you must access the original class something like

class SomeClass {
    public int InstanceMethod() { return 1; }
    public static int StaticMethod() { return 42; }
}

Now you can use StaticMethod as:

SomeClass.StaticMethod();  

Then you must access to Microsoft .net framework code to add IsAddOp(x) method to Char class. Actually your way to define a extension with that one you say in question is wrong.. you dont try to define Extension method..you try to define Static method.

Ali
  • 3,373
  • 5
  • 42
  • 54