3

I have my own Math library and I want it to be called "Math" (I'd been calling 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 to add a wrapper for everything in System.Math to my library that just calls the System.Math function explicitly, and then I have to add

using Math = Yushatak.Libraries.Math;

to every file that uses Math.* functionality. I don't feel like this is the best way to do this, and I also fear that the wrapping will cause extra overhead, and that's not someplace where you want overhead either..

Advice? Is there a better way to "extend" System.Math? Is this just a bad idea and I should go back to "Maths"? Any suggestions at all? :P

Example of wrapped method:

    public static decimal Abs(decimal value)
    {
        return System.Math.Abs(value);
    }
Yushatak
  • 741
  • 1
  • 5
  • 15
  • 4
    Maybe a better fit for code review SE? Hard to comment on overhead without seeing the code. If it's mostly static methods then you're probably OK. Nothing wrong with the `using` statement above either; resolving namespace collisions is exactly why that syntax exists. – Yuck Oct 28 '14 at 14:15
  • 3
    This question appears to be off-topic because it is part opinion-based and part code review. – CodeCaster Oct 28 '14 at 14:16
  • 2
    Depending on what your `Math/Maths` class is doing it may be best to just extend the `System.Math` class – Nunners Oct 28 '14 at 14:16
  • Added an example - they are just static methods. @Nunners: How could I "just extend" the System.Math class? That sounds like what I want to do, but I thought I could only literally extend a class when it was declared partial in the first place? – Yushatak Oct 28 '14 at 14:20
  • Try looking at this SO Question: http://stackoverflow.com/questions/3424458/should-i-use-otherwise-optimal-class-names-that-conflict-with-the-net-bcls-n – Icemanind Oct 28 '14 at 14:20
  • 1
    As for your performance question: [using directives are resolved at compile-time](http://stackoverflow.com/questions/136278/why-should-you-remove-unnecessary-c-sharp-using-directives). – CodeCaster Oct 28 '14 at 14:23
  • It's not the using directives I am concerned with for performance - it's the added method call. I.e., I call Math.Abs(myIntegerValue); and it calls Yushatak.Math.Abs(int value), which then calls System.Math.Abs(int value) - two resolutions instead of one. In most cases not relevant, but real-time applications like games and etc. it could be unless the compiler knows to boil that down and remove the added call. – Yushatak Oct 28 '14 at 14:28
  • @icemanind: that question does cover some similar ground but it looks like it just ended up with the answer of "deal with it, it's a decision to make" (which doesn't help) or "use extension methods", the latter of which isn't relevant to this particular case AFAIK. – Yushatak Oct 28 '14 at 14:29

1 Answers1

5

This answer now has three distinct parts.

While the using statement approach does not have any overhead aside from one extra line in your source code, the wrapper methods would have overhead in a number of ways.

  1. There are a very large number of methods in System.Math. Accurately reproducing the wrappers would be tedious at best.
  2. The performance overhead at runtime could range from insignificant to severe, depending on how the JIT handles calls to math functions internally. This could vary by both implementation and platform.
  3. The compiled code for your class will be larger.
  4. It will be harder to understand which functionality in your math class is new functionality, and which is simply a wrapper around something that already existed.

All of the above can be prevented by using a different strategy.


It is fairly common practice to intentionally avoid the name conflict my naming the new class NameEx, where Name is the original name. For example, you would be interested in creating the class MathEx.


You can also use something like the following to handle conflicting names.

using Math = Yushatak.Libraries.Math;
using SystemMath = System.Math;

This frequently happens in Visual Studio extension development, in particular with classes named Constants and IServiceProvider.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • OK this rules out the wrapper idea for the same reasons I suspected it should be ruled out. I already wrote it, though, so #1 was out the door, lol.. Unless someone comes up with a better solution than basically my original "don't use the name" (your MathEx), in which I was using "Maths" (the shortest still-correct name for the functionality that isn't taken), I'll mark this as the answer.. – Yushatak Oct 28 '14 at 14:35