0

...is it possible?

For example, can I add this simple function...

public static double Deg2Rad(double degrees) {
    return Math.PI / 180 * degrees;
}

...to the Convert class? So (using the default..."usings") you can call

double radians = Convert.Deg2Rad(123);

Can this be done? If so, how?

Mark Lalor
  • 7,820
  • 18
  • 67
  • 106

5 Answers5

1

No, it's not possible. You could use extensions if Convert weren't static, but no.

You could add an extension to double though, using the this keyword:

public static double Deg2Rad(this double degrees) {
    return Math.PI / 180 * degrees;
}

And use it like so:

double radians = (123D).Deg2Rad();

You'll have to put your method in a static class for it to work, though.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1

You can sort of get what you want. C# has "Extension Methods". This allow you to add methods to another class, even if that class is in another assembly that you do not have the source code for. However, you cannot add static functions to another class. You can only add instance methods.

For more information, see MSDN Extensions Methods.

Lee Berger
  • 1,234
  • 14
  • 21
1

No you can't, but you can add an Extension method to double and call it like

double x = 180.0;
double y = Math.Cos( x.Deg2Rad() );
double z = Math.Sin( 0.5.Pi() );

in an static class add the following extension method:

public static class ExtensionMethods
{
    public static double Deg2Rad(this double angle)
    {
        return Math.PI*angle/180.0;
    }

    public static double Pi(this double x)
    {
        return Math.PI*x;
    }
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • Accepted, also is there a way to make this apply for ints too without rewriting it with int? – Mark Lalor Apr 28 '11 at 23:22
  • You can cast it as double: `x = ((double)i).Pi()` but it is ugly. Just go ahead and write extensions for the types you want. You can place the extensions in different namespaces, so depending what `using` statement you use, different extensions will be available. – John Alexiou Apr 28 '11 at 23:29
1

No you can't, but you don't really need to - you can just declare your own static class instead, for example:

public static class MyConvert
{
    public static double Deg2Rad(double degrees) 
    {
        return Math.PI / 180 * degrees;
    }
}

Usage:

double radians = MyConvert.Deg2Rad(123);

(Obviously MyConvert is a rubbish name, but you get the idea).

The only difference between the above and having the method on the system Convert class is that if its on the Convert class it looks like a built-in function, but you are going to struggle to convince me thats actually a good thing (I like to know when I'm calling framework code vs code maintained internally).

Justin
  • 84,773
  • 49
  • 224
  • 367
-1

Apart from what has been said here, there is also another option which is Partial classes (this won't apply to Convert).

If the class is declared partial, then you can add menthods via another partial class, even if it's in another assembly (DLL).

But, again, the class has to be originally declared partial for that to work.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • I know this is old, but just to be sure others don't get misled by this, I am downvoting and posting a link showing that partial classes cannot be used across of assemblies: http://stackoverflow.com/a/647459/16241 – Vaccano Feb 17 '17 at 00:08