C# compiler number literals?

From the C# specification 2.4.4.2 Integer literals and 2.4.4.3 Real literals. Take note that L and UL are preferred as opposed to their lowercase variants for clarity as recommended by Jon Skeet .

Up vote 13 down vote favorite 5 share g+ share fb share tw.

By default declaring '0' makes it an Int32 and '0.0' makes it a 'Double'. I can use the literal modifier 'f' at the end to ensure something is treated as a 'Single' instead. For example like this... var x = 0; // x is Int32 var y = 0f; // y is Single What are the other modifiers I can use?

Is there one for forcing to Double, Decimal, UInt32? I tried googling for this but could not find anything. Maybe my terminology is wrong and so that explains why I am coming up blank.

Any help much appreciated. C# compiler link|improve this question asked Oct 3 '08 at 13:09Phil Wright8,18923063 99% accept rate.

Var y = 0f; // f is single var z = 0d; // z is double var r = 0m; // r is decimal var I = 0U; // I is unsigned int var j = 0L; // j is long (note capital L for clarity) var k = 0UL; // k is unsigned long (note capital L for clarity) From the C# specification 2.4.4.2 Integer literals and 2.4.4.3 Real literals. Take note that L and UL are preferred as opposed to their lowercase variants for clarity as recommended by Jon Skeet.

3 While l is certainly valid for longs, I'd recommend using L instead - it's just as valid, and doesn't look like a "1" – Jon Skeet Oct 3 '08 at 13:54 @Jon Skeet: bad habit of a C programmer, added your advice. – sixlettervariables Oct 3 '08 at 15:35.

If you don't want to have to remember them, then the compiler also accepts a cast for the same purpose (you can check the IL that the effect is the same - i.e. The compiler, not the runtime, does the cast). To borrow the earlier example: var y = (float)0; // f is single var z = (double)0; // z is double var r = (decimal)0; // r is decimal var I = (uint)0; // I is unsigned int var j = (long)0; // j is long var k = (ulong)0; // k is unsigned long And for the record, I agree that "var" is a bad choice here; I'll happily use var for a SortedDictionary, but for "int" it is just lazy...

Marc Gravell: +1 good advice using the cast, but sometimes you need to use the suffix (say, bad example, 3.14159m). I personally use the cast when I'm looking to be explicit. – sixlettervariables Oct 3 '08 at 13:24 The compiler treat the following identically; do you have an example where it makes a difference?

Var x = 3.14159m; var y = (decimal)3.14159; – Marc Gravell? Oct 3 '08 at 13:26 @Marc Gravell: hmm, not that I can think of, just the hunch of a C/C++ programmer. I guess I don't take the same things for granted!

Thanks for the tip. – sixlettervariables Oct 3 '08 at 16:08 3 To answer the question in the comment, (decimal)1.000000000000001 evaluates to 1 whereas 1.000000000000001m evaluates to 1.000000000000001. In the former case, the computer parses the literal as a double and then casts it to a decimal.

– Joe Albahari Oct 3 '08 at 6:02.

You might want to start by looking at the C# language spec. Most of the types are listed in there, and have a suffix: L = long F = float U = uint ulong's are a little different m = decimal (money) D = double Of course, if you stop using var then you get around the whole problem, and your code becomes more readable (ok, thats subjective, but for something like this, it's more readable by other people: var x = 0; //whats x? Float x = 0; //oh, it's a float byte x = 0; // or not!

Nic Wise: +1 stop using var ;D Although it does simplify showing which suffix goes with which type. – sixlettervariables Oct 3 '08 at 13:18 yeah, very good point :) I like var in general, esp in foreach statements (foreach(var a in myList)) but other than that, I'm not a huge fan. – Nic Wise Oct 3 '08 at 13:22.

While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – Bill the Lizard? Dec 29 '11 at 18:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions