I believe that what you are looking for is called Extension Methods .
See more at: extension !
Using this concept, you can create extensions for all classes and structures, as long as they are not static.
The Math class is static and can not be extended:
Theotheranswersevenseemtowork,butinfactyouwouldbeaccessinganewclassandnottheexistingone:System.Math
.
Seethedifferencebelow:
Listofmethodsofthenewclass,comparedtothelistofmethodsofclassSystem.Math
below:
Here'sanexampleofwhatadoubleextensionmightlookliketogetanidea:
classProgram{publicstaticvoidMain(){doubledez=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