How to temporarily replace one primitive type with another when compiling to different targets in c#

Posted by Keith on Stack Overflow See other posts from Stack Overflow or by Keith
Published on 2009-08-26T16:14:05Z Indexed on 2010/04/19 1:33 UTC
Read the original article Hit count: 413

Filed under:
|
|
|
|

How to easily/quickly replace float's for doubles (for example) for compiling to two different targets using these two particular choices of primitive types?

Discussion: I have a large amount of c# code under development that I need to compile to alternatively use float, double or decimals depending on the use case of the target assembly.

Using something like “class MYNumber : Double” so that it is only necessary to change one line of code does not work as Double is sealed, and obviously there is no #define in C#. Peppering the code with #if #else statements is also not an option, there is just too much supporting Math operators/related code using these particular primitive types.

I am at a loss on how to do this apparently simple task, thanks!

Edit: Just a quick comment in relation to boxing mentioned in Kyles reply: Unfortunately I need to avoid boxing, mainly since float's are being chosen when maximum speed is required, and decimals when maximum accuracy is the priority (and taking the 20x+ performance hit is acceptable). Boxing would probably rules out decimals as a valid choice and defeat the purpose somewhat.

Edit2: For reference, those suggesting generics as a possible answer to this question note that there are many issues which count generics out (at least for our needs). For an overview and further references see Using generics for calculations

© Stack Overflow or respective owner

Related posts about c#

Related posts about replace