Numeric suffixes - CSharp Language Basics

CSharp examples for Language Basics:Number

Introduction

Numeric suffixes explicitly define the type of a literal. Suffixes can be either lower- or uppercase, and are as follows:

Category C# typeExample
F float float f = 1.0F;
D double double d = 1D;
M decimal decimal d = 1.0M;
U uint uint i = 1U;
L long long i = 1L;
ULulong ulong i = 1UL;

The F and M suffixes should always be applied when specifying float or decimal literals.

Without the F suffix, the following line would not compile, because 4.5 would be inferred to be of type double, which has no implicit conversion to float:

Demo Code

using System;//from  w w  w  .  j  a  v  a2s  .  c  o m
class Test
{
   static void Main(){
      float f = 4.5F;
      Console.WriteLine (f);
   }
}

Result

The same principle is true for a decimal literal:

Demo Code

using System;/*w ww .  ja  v a2 s  . co m*/
class Test
{
static void Main(){
     decimal d = -1.23M;     // Will not compile without the M suffix.
     Console.WriteLine (d);

}
}

Result


Related Tutorials