Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you extend (or CAN you extend) the static Math methods?

With C# 3.0, I know you can extend methods using the 'this' nomenclature.

I'm trying to extend Math.Cos(double radians) to include my new class. I know that I can just create a "Cos" method in my existing class, but I'm just interested in seeing how/if this can be done for the sake of the exercise.

After trying a few new things, I'm returning to SO to get input. I'm stuck.

Here's what I have at this point...

public class EngMath
{
    /// ---------------------------------------------------------------------------
    /// Extend the Math Library to include EngVar objects.
    /// ---------------------------------------------------------------------------

    public static EngVar Abs(this Math m, EngVar A)
    {
        EngVar C = A.Clone();

        C.CoreValue = Math.Abs(C.CoreValue);

        return C;
    }

    public static EngVar Cos(this Math m, EngVar A)
    {
        EngVar C = A.Clone();
        double Conversion = 1;
        // just modify the value. Don't modify the exponents at all

        // is A degrees? If so, convert to radians.
        if (A.isDegrees) Conversion = 180 / Math.PI;

        C.CoreValue = Math.Cos(A.CoreValue * Conversion);

        // if A is degrees, convert BACK to degrees.
        C.CoreValue *= Conversion;

        return C;
    }

    ...
like image 882
Jerry Avatar asked Jan 27 '09 18:01

Jerry


People also ask

Can you extend a static class?

Just like static members, a static nested class does not have access to the instance variables and methods of the outer class. You can extend static inner class with another inner class.

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.

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.

Can extension methods extend non static classes?

It is compulsion that the Extension method must be in a Static class only so that only one Instance is created. For example, if you place the following in ASP.Net page it will not work. Though error will not come, but you will not see the method available.


1 Answers

Extension methods are a way to make your static methods appear to be instance methods on the type they "extend". In other words you need an instance of something in order to use the extension method feature.

It sound to me that you're going about it in the opposite way by trying to make Math.Cos handle your type. In that case, I'm afraid you have to implement the functionality yourself. If that is not what you're trying to do, please clarify.

like image 136
Brian Rasmussen Avatar answered Sep 29 '22 11:09

Brian Rasmussen