C# Numeric Literals

In this chapter you will learn:

  1. Integral literals
  2. Real literals
  3. Numeric literal type inference

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  ww .j a v  a2  s .  co  m

Next chapter...

What you will learn in the next chapter:

  1. What C# Numeric suffixes
  2. Example for C# Numeric suffixes
Home »
  C# Tutorial »
    C# Language »
      C# Data Types
C# Predefined Type Taxonomy
C# Numeric Types
C# Numeric Literals
C# Numeric suffixes
C# float and double
C# Float and Double Special Values
C# decimal
C# Numeric Conversions
C# Bool Type
C# char type
C# String Type