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:
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:
List of methods of the new class compared to the list of methods of the class System.Math
down below :
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
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...– Guilherme Branco Stracini