12

Assume i want to create an alias of a type in C# using a hypothetical syntax:

 Currency = float;

Then i go away and create a few thousand files that use Currency type.

Then i realize that i prefer to use FCL types:

 Currency = System.Single;

Excellent, all code still works.

...months later...

Wait, i'm getting some strange rounding errors. Oh that's why, System.Single only has 7 digits of precision. Lets up that to 15 digits:

 Currency = System.Double;

...years later...

Ohhhh, floating point isn't exact; multiplying $0.0011/unit * 217,384 units exposes some limitations of using floating point. And accountants are sticklers against "accounting irregularities". No problem:

Currency = System.Decimal;

...years later...

International applications? Currency codes. Hmmmm. Thank you CodeProject:

 Currency = Money;

...later...

Ooo, patterns and practices. Let's obfuscate some of that code:

 Currency = ICurrency;

And during all this nonsense code didn't break.

i know C# doesn't support this level of encapsulation and resilency with the syntax i made up.

But can anyone suggest a syntax that can simulate what people have been trying to accomplish (including myself)?

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

2 Answers2

5

Create a class called Currency and implement (or delegate) the appropriate operators, then just change the class used to store the data internally when desired.

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
4

You can use using like so: using Currency = System.Single;, but you must do it in every single file. But still easier to change, than searching for single in whole application.

Krzysztof
  • 15,900
  • 2
  • 46
  • 76