Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Literal number suffixes in C#
Literal number suffixes in C# are used to explicitly specify the data type of numeric literals. Without suffixes, the compiler infers the type based on the value, but suffixes ensure the literal is treated as a specific numeric type.
These suffixes are particularly useful when working with method overloads, preventing ambiguous type conversions, and ensuring the correct data type is used for calculations.
Syntax
Following is the syntax for using literal number suffixes −
dataType variable = numericValue + suffix;
The suffix can be either uppercase or lowercase, but uppercase is recommended for clarity −
long val1 = 12345L; // L for long float val2 = 3.14F; // F for float decimal val3 = 99.99M; // M for decimal
Common Literal Number Suffixes
| Data Type | Suffix | Example | Without Suffix (Default) |
|---|---|---|---|
| long | L or l | 12345L | int (if fits) |
| ulong | UL or ul | 12345UL | int (if fits) |
| uint | U or u | 12345U | int (if fits) |
| float | F or f | 3.14F | double |
| double | D or d | 3.14D | double (default for decimals) |
| decimal | M or m | 3.14M | double |
Using Integer Suffixes
Example
using System;
public class Program {
public static void Main() {
// long suffix
long val1 = 29345L;
Console.WriteLine("Long: " + val1);
// unsigned int suffix
uint val2 = 3456U;
Console.WriteLine("Unsigned int: " + val2);
// unsigned long suffix
ulong val3 = 987654321UL;
Console.WriteLine("Unsigned long: " + val3);
// Demonstrating the difference
Console.WriteLine("Type of 123: " + 123.GetType());
Console.WriteLine("Type of 123L: " + 123L.GetType());
}
}
The output of the above code is −
Long: 29345 Unsigned int: 3456 Unsigned long: 987654321 Type of 123: System.Int32 Type of 123L: System.Int64
Using Floating-Point Suffixes
Example
using System;
public class Program {
public static void Main() {
// float suffix - required for float literals
float val1 = 250.35F;
Console.WriteLine("Float: " + val1);
// double suffix - optional since double is default
double val2 = 297.325D;
Console.WriteLine("Double: " + val2);
// decimal suffix - required for decimal literals
decimal val3 = 3245.5678M;
Console.WriteLine("Decimal: " + val3);
// Showing precision differences
Console.WriteLine("Float precision: " + (1.0F/3.0F));
Console.WriteLine("Double precision: " + (1.0/3.0));
Console.WriteLine("Decimal precision: " + (1.0M/3.0M));
}
}
The output of the above code is −
Float: 250.35 Double: 297.325 Decimal: 3245.5678 Float precision: 0.33333334 Double precision: 0.3333333333333333 Decimal precision: 0.3333333333333333333333333333
Why Use Suffixes
Suffixes prevent compilation errors and ensure the correct data type is used, especially in method overloading scenarios −
Example
using System;
public class Calculator {
public static void Calculate(float value) {
Console.WriteLine("Float method called: " + value);
}
public static void Calculate(double value) {
Console.WriteLine("Double method called: " + value);
}
public static void Main() {
// Without suffix - calls double method (default)
Calculate(3.14);
// With F suffix - calls float method
Calculate(3.14F);
// With D suffix - explicitly calls double method
Calculate(3.14D);
}
}
The output of the above code is −
Double method called: 3.14 Float method called: 3.14 Double method called: 3.14
Conclusion
Literal number suffixes in C# explicitly specify the data type of numeric literals, preventing ambiguity and ensuring the correct type is used. Common suffixes include L for long, F for float, D for double, M for decimal, and U for unsigned types. Using suffixes is essential for method overloading scenarios and when precise numeric types are required.
