How to add methods to a native class?

Asked

Viewed 287 times

4

How to add functions in a native C#class, in my case I tried to do in System.Math only as a test:

public class Math
{
    public static double test(double a)
    {
        return a;
    }
}

And call it that:

Math.test(10);

But I did not get the expected result. How would it be possible to add the method in this class?

  • You called Math.test(10) simply ? Put the result in a variable ? Or saved it in a log file or displayed it in the/trace console ? What did you do with the result ? Generated any compiler error? Generated any exception in Runtime ? Here’s an example I just made based on your: http://rextester.com/YZX58485 Has 2 methods, Double and Half, as the name already suggests, Double multiplies the value by 2 and Half divides by 2, worked perfectly...

2 answers

4


This is not possible today, and it has little advantage to do this. Create a new class and good.

public class MathExt {
    public static double test(double a) => a;
}

MathExt.test(10);

I put in the Github for future reference.

Is in studies allow Extension Everything, and there may exist static extension methods, in theory could be in static classes.

  • I will emphasize that there is no way to extend a static class, as you want to know the question. The accepted answer is extending the class Float32 and not Math, how is the question. I get my answer, because it answers what was asked. If the desire was to ask other things, should have been asked, so I closed the question, the intention was one, but the text was another.

  • Okay, I realized that your answer was really the right one and the one that most suited what I really wanted, I marked it as correct, thank you for explaining Rodolpho’s answer. I could now take the mark?

  • @Francis I didn’t close because you accepted another answer. I need to evaluate better because it is still confused what you really want, the closure of unclear is because you do not know exactly what is being asked, and not what the answer accepts. It got complicated because it has well-voted antagonistic responses. This sends the wrong message, shows that the question left room for misinterpretations, ie, this is the definition of unclear. I thought I understood why you were talking about Math. I guess I shouldn’t have responded and closed up before I got answers.

  • "It got complicated because it has well-voted antagonistic responses." This has nothing to do, people may have voted only because the answer was well prepared and showed examples of use. The question has no errors, describes exactly what my problem, not to understand it the person has to be very dumb.

4

I believe what you’re looking for is called Extension Methods.

See more in : Extension methods !

Using this concept, you can create extensions for all classes and structures, as long as they are not static.

Math class is static and cannot be extended:

Erro no Visual Studio

The other answers seem to work, but in fact you would be accessing a new class and not the existing one :System.Math.

See the difference below:

Classe Nova

List of methods of the new class compared to the list of methods of the class System.Math down below :

inserir a descrição da imagem aqui

Below is an example of how a double extension could be for you to get an idea:

class Program
{
    public static void Main()
    {
        double dez = 10;
        Console.WriteLine("Dobro(10) => {0}", dez.Dobro());
        Console.WriteLine("Metade(10) => {0}", dez.Metade());             
    }    
}

public static class DoubleEx
{
    public static double Dobro(this double a)
    {
        return a * 2;
    }

    public static double Metade(this double a)
    {
        return a / 2;
    }
}

With the following output: Double(10) => 20 Half(10) => 5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.