Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to write an Extension Method that applies to multiple types?

I'm trying to write an extension method that will add the function HasFactor to the class int in C#. This works wonderfully, like so:

static class ExtendInt
{
    public static bool HasFactor(this int source, int factor)
    {
        return (source % factor == 0);
    }
}

class Program
{
    static void Main()
    {
        int i = 50;
        int f = 2;
        bool b = i.HasFactor(f);
        Console.WriteLine("Is {0} a factor of {1}? {2}",f,i,b);
        Console.ReadLine();
     }
}

This works great because variable i in the Main() method above is declared as an int. However, if i is declared as an Int16 or an Int64, the extension method does not show up unless it is explicitly cast as an int or Int32.

I now would like to apply the same HasFactor method to Int16 and Int64. However, I'd rather not write separate extension methods for each type of int. I could write a single method for Int64 and then explicitly cast everything as an Int64 or long in order for the extension method to appear.

Ideally, I'd prefer to have the same extension method apply to all three types without having to copy and paste a lot of code.

Is this even possible in C#? If not, is there a recommended best-practice for this type of situation?

like image 998
Ben McCormack Avatar asked Jan 01 '10 23:01

Ben McCormack


People also ask

What is the difference between a static method and an extension method?

The only difference between a regular static method and an extension method is that the first parameter of the extension method specifies the type that it is going to operator on, preceded by the this keyword.

Do extension methods have to be static?

An extension method must be a static method. An extension method must be inside a static class -- the class can have any name. The parameter in an extension method should always have the "this" keyword preceding the type on which the method needs to be called.

Can you extend methods?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

Can you add extension methods to an existing static class?

No. Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don't need an extension method since you can just add the method directly.


1 Answers

No, this is not possible in C#. You'll have to create one extension method for each type. The closest is to have an extension method that operates on an interface, but in this case there is no INumeric interface or anything similar that is implemented by the various numeric types.

like image 144
Eilon Avatar answered Oct 07 '22 16:10

Eilon