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
Lowercase suffixes in C#
In C#, lowercase suffixes are used with numeric literals to specify their data type explicitly. These suffixes tell the compiler to treat the literal as a specific numeric type rather than inferring the type automatically.
Syntax
Following are the common lowercase suffixes used with numeric literals −
long number = 12345l; // l for long float number = 3.14f; // f for float uint number = 100u; // u for unsigned int ulong number = 500ul; // ul for unsigned long
Common Lowercase Suffixes
| Suffix | Data Type | Example |
|---|---|---|
| l | long | 29876l |
| f | float | 95.10f |
| u | uint (unsigned int) | 100u |
| ul | ulong (unsigned long) | 500ul |
| d | double | 3.14d |
| m | decimal | 19.99m |
Using Lowercase Suffixes
Example
using System;
public class Program {
public static void Main() {
long a = 29876l;
float b = 95.10f;
uint c = 100u;
ulong d = 500ul;
Console.WriteLine("Long value: " + a);
Console.WriteLine("Float value: " + b);
Console.WriteLine("Unsigned int: " + c);
Console.WriteLine("Unsigned long: " + d);
// Display types
Console.WriteLine("Type of a: " + a.GetType());
Console.WriteLine("Type of b: " + b.GetType());
}
}
The output of the above code is −
Long value: 29876 Float value: 95.1 Unsigned int: 100 Unsigned long: 500 Type of a: System.Int64 Type of b: System.Single
Compiler Warning with 'l' Suffix
The lowercase l suffix can be easily confused with the digit 1, which may lead to readability issues. The compiler often generates a warning suggesting to use uppercase L for clarity −
Example
using System;
public class Program {
public static void Main() {
// Lowercase 'l' - may cause confusion
long confusing = 12345l;
// Uppercase 'L' - clearer and recommended
long clear = 12345L;
Console.WriteLine("Confusing: " + confusing);
Console.WriteLine("Clear: " + clear);
Console.WriteLine("Both are equal: " + (confusing == clear));
}
}
The output of the above code is −
Confusing: 12345 Clear: 12345 Both are equal: True
Without Suffixes vs With Suffixes
Example
using System;
public class Program {
public static void Main() {
// Without suffix - compiler infers type
var number1 = 3.14; // inferred as double
var number2 = 100; // inferred as int
// With suffix - explicit type specification
float floatNum = 3.14f;
uint uintNum = 100u;
Console.WriteLine("Inferred double: " + number1.GetType());
Console.WriteLine("Inferred int: " + number2.GetType());
Console.WriteLine("Explicit float: " + floatNum.GetType());
Console.WriteLine("Explicit uint: " + uintNum.GetType());
}
}
The output of the above code is −
Inferred double: System.Double Inferred int: System.Int32 Explicit float: System.Single Explicit uint: System.UInt32
Conclusion
Lowercase suffixes in C# help specify exact data types for numeric literals, preventing type inference issues and ensuring the compiler treats literals as intended. While functional, using uppercase suffixes like L instead of l is recommended for better code readability and to avoid compiler warnings.
