Search code examples
c#typesconstructorint128

C# Custom data type!


Possible Duplicate:
Int128 in .Net?

After I decided to implement my Int128 in C#, I thought it would be nice to make it look like other dotNet data types.. But I could not implement the following feature:

  • suffix initialization: such as 13L and 0.2D

Can I make my own suffix in C#?
And if I can not.. how can I initialize it?
i.e

Int128 a= ??

Solution

  • You cannot create your own suffix initializers - this is part of the language and baked into the compiler. If you write your own C# compiler, on the other hand, you can do whatever you want and extend it in whichever way you wish.

    As for how you would initialize it - it depends on how you wrote it. If it is a class, you would need to initialize it with the new keyword, or write some implicit conversions to numeric types that have less precision.

    As Jon noted, you will have no way to actually set it to a value larger than ulong.MaxValue without some string manipulation. You will also have to represent in internally in some form (string? A number of ulongs?) and make sure all conversions and operations work correctly (what about overflows?).