Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Custom data type! [duplicate]

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= ??
like image 903
Betamoo Avatar asked May 12 '10 14:05

Betamoo


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

No, you can't create your own form of literal in C#.

You could have an implicit conversion from int, long, uint and ulong, allowing:

Int128 a = 100;

but you wouldn't be able to specify values greater than ulong.MaxValue that way.

Having an implicit conversion from decimal would allow a larger range, but would be a bad idea because you would lose data for any value which wasn't just an integer.

Basically there's nothing which will allow a literal guaranteed-to-be-integer value greater than ulong.MaxValue - you could parse a string, but that's not really ideal. Alternatively you could create a constructor which took two ulongs, one for the bottom 64 bits and one for the top 64 bits.

like image 160
Jon Skeet Avatar answered Oct 27 '22 02:10

Jon Skeet


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?).

like image 37
Oded Avatar answered Oct 27 '22 03:10

Oded


No, you can't do suffix initialisation. That bit of cuteness will be available in the forthcoming C++ standard ... but I digress.

The most obvious choices are Int128(long hi, ulong lo) and Int128(string).

like image 21
Marcelo Cantos Avatar answered Oct 27 '22 02:10

Marcelo Cantos