C# Numeric Literals

Integral literals

Integral literals can use decimal or hexadecimal notation; hexadecimal is denoted with the 0x prefix. For example:


int x = 127; 
long y = 0x7F; 

Real literals

Real literals can use decimal and/or exponential notation. For example:


double d = 1.5; 
double million = 1E06; 

Numeric literal type inference

By default, the 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.
  • Otherwise, the literal's type is the first type in this list that can fit the literal's value: int, uint, long, and ulong.

For example:


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) 
//from   w w  w.j a  v a2  s  . co m




















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var