Numeric Literals - CSharp Language Basics

CSharp examples for Language Basics:Number

Introduction

Integral literals can use decimal or hexadecimal notation.

hexadecimal is denoted with the 0x prefix. For example:

Demo Code

using System;//www. j a  v a2 s  .co m
class Test
{
   static void Main(){
      int x = 127;
      long y = 0x7F;
      Console.WriteLine (x);
      Console.WriteLine (y);
   }
}

Result

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

Demo Code

using System;/* w w w  .jav a 2 s.c om*/
class Test
{
static void Main(){
     double d = 1.5;
     double million = 1E06;

     Console.WriteLine (d);
     Console.WriteLine (million);

}
}

Result

Numeric literal type inference

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:

Demo Code

using System;//from   w w  w  .j  a  va 2s .c om
class Test
{
static void Main(){
     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)
}
}

Result


Related Tutorials