Is packaging System.Math a bad idea?
I have my own math library and I want to be called "Math" (I called it "Maths"). It's in its own namespace, but the class name being "Math" still conflicts with System.Math. What I did to solve this was add a wrapper for everything in System.Math to my library, which just calls the System.Math function and then I have to add
using Math = Yushatak.Libraries.Math;
for each file using Math functions. *. I don't feel like this is the best way to do it, and I am also worried that the wrapper will cause additional overhead and this is not where you want the overhead ..
Tips? Is there a better way to "extend" System.Math? Is this just a bad idea and should I go back to Math? Any suggestions at all?: P
An example of a wrapped method:
public static decimal Abs(decimal value)
{
return System.Math.Abs(value);
}
source to share
This answer now has three parts.
While a subroutine using
has no overhead other than one extra line in the source code, wrapper methods will have overhead in several ways.
- There
System.Math
are many methods. Exact copying of the wrappers will be tedious at best. - Runtime performance overhead can range from minor to major, depending on how the JIT handles math calls internally. This can vary in implementation and platform.
- The compiled code for your class will be larger.
- It will be harder to figure out what functionality in your math class is new functionality and is just a wrapper around what already exists.
All of the above can be prevented with a different strategy.
It is a common practice to deliberately avoid naming conflicts NameEx
when naming a new class where Name
is the original name. For example, you might be interested in creating a class MathEx
.
You can also use something like the following to handle conflicting names.
using Math = Yushatak.Libraries.Math;
using SystemMath = System.Math;
This occurs frequently in the development of a Visual Studio extension, in particular with classes named Constants
and IServiceProvider
.
source to share