C# Suffix Examples

Suffixes specify number types. They instruct the C# compiler that an integral literal such as 1000 be considered a certain type of number: for example, a long with 1000L. We look into how you can add numeric suffixes to numbers—and avoid warnings related to these suffixes.

Screenshot of numeric suffixes

This C# article looks at suffixes on literals. It provides an example of the syntax.

Example

First, numeric suffixes, which are also called literal number suffixes, are hints to the compiler that a literal number is of a certain type. Recall that "literal" means a value hard-coded into your program. My investigation revealed that literal suffixes on constants actually generate conv instructions. This means they work the same as runtime casts.

Program that uses literal number suffixes [C#]

using System;

class Program
{
    static void Main()
    {
	// Use long suffix.
	long l1 = 10000L;

	// Use double suffix.
	double d1 = 123.764D;

	// Use float suffix.
	float f1 = 100.50F;

	// Use unsigned suffix.
	uint u1 = 1000U;

	// Use decimal suffix.
	decimal m2 = 4000.1234M;

	// Use unsigned suffix and long suffix.
	ulong u2 = 10002000300040005000UL;
    }
}
Note

Literal suffixes. The letters L, D, F, U, M and UL are appended to the end of the integral literals in the declarations in the Main entry point above. This instructs the C# compiler that 10000 is a long type, not an int type. The MSIL generated is similar to using casts such as (long)10000.

Review

Here we look at the suffixes in more detail, in a table that tells you what the letters actually stand for. We also see examples of the suffixes in the C# programming language.

Suffix type: unsigned int
Character:   U
Example:     uint x = 100U;

Suffix type: long
Character:   L
Example:     long x = 100L;

Suffix type: unsigned long
Character:   UL
Example:     ulong x = 100UL;

Suffix type: float
Character:   F
Example:     float x = 100F;

Suffix type: double
Character:   D
Example:     double x = 100D;

Suffix type: decimal
Character:   M
Example:     decimal x = 100M;

Lowercase suffixes

Programming tip

In the C# programming language, you can also specify lowercase suffixes, such as u, l, ul, f, d, and m. However, these are easier to confuse with numbers. For example, the letter 'l' is very commonly seen as the number 1. Visual Studio recommends that you use uppercase casts for "l", warning that "The 'l' suffix is easily confused with the digit '1'—use 'L' for clarity." The screenshot shows this warning in the IDE.

Examples of lowercase suffixes

Lowercase suffix: long x = 10000l; // Is that 100001 or 10000l?
Uppercase suffix: long x = 10000L; // It's 10000L.

Summary

The C# programming language

We saw how you can use suffixes on the literal numbers in your C# programs, which is a way to tell the C# compiler that you want the literal number to be treated as a certain type of number. The C# language specification defines all the rules for how numbers are converted when used as operands, but these suffixes can force certain conversions.

Numeric Casts Number Examples
.NET