CSharp - Numeric literal type inference


C# compiler infers a numeric literal to be either double or an integral type:

If the literal contains a decimal point or the exponential symbol (E), it is a double.

Then it check if the value can be fit in an type in the following list:int, uint, long, and ulong.

For example:1.0 will be double by default since it has decimal part.

4 is a int type.

The following code outputs the inferred type for each literal.


using System;

class MainClass/*  w w  w  . ja  va  2  s . c  o  m*/
   public static void Main(string[] args)

     Console.WriteLine (        1.0.GetType());  // Double  (double)
     Console.WriteLine (       1E06.GetType());  // Double  (double)
     Console.WriteLine (          1.GetType());  // Int32   (int)
     Console.WriteLine ( 0xF0000000.GetType());  // UInt32  (uint)
     Console.WriteLine (0x100000000.GetType());  // Int64   (long)