CSharp - Data Types Numeric Literals

Introduction

Integral-type literals can use decimal or hexadecimal notation.

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

int x = 127;
long y = 0x7F;

You can insert an underscore anywhere inside a numeric literal to make it more readable:

int million = 1_000_000;

You can specify numbers in binary with the 0b prefix:

var b = 0b1010_1011_1100_1101_1110_1111;

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

double d = 1.5;
double million = 1E06;

The following code outputs the value we just assigned to variable using literals.

Demo

using System;

class MainClass//from w w w. j  a  va2 s .co m
{
   public static void Main(string[] args)
   {
      int x = 127;
      long y = 0x7F;
      Console.WriteLine(x);
      Console.WriteLine(y);

      var b = 0b1010_1011_1100_1101_1110_1111;
      Console.WriteLine(b);
   
      double d = 1.5;
      double million = 1E06;

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

Result

Quiz