Home
Search
C# Numeric Suffix ExamplesUnderstand the usage of suffixes on literals. See examples of numeric suffix syntax.
Suffix. A suffix specifies a number's type. They instruct the C# compiler that an integral literal such as 1000 be considered a certain type of number—for example, a long (1000L).
C# syntax info. We look into how you can add numeric suffixes to numbers. And we explore how these suffixes are compiled—they are treated like casts.
Long
Casts
Example code. To begin, numeric suffixes are also called literal number suffixes. They are hints to the compiler that a literal number is of a certain type.
Literal Recall that "literal" means a value hard-coded into your program. The letters are appended to the end of the literals.
Note Literal suffixes on constants generate conv instructions. This means they work the same as runtime casts.
And An "L" instructs the C# compiler that 10000 is a long type. The IL generated is similar to using casts such as (long).
C# program that uses literal number suffixes
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; } }
This table indicates the meaning of the suffix letters. We also see examples of the suffixes in the C# language—the code statements can be used within programs.
Syntax table:
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. You can also specify lowercase suffixes. But these are easier to confuse with numbers—the letter "l" is sometimes seen as the number 1. It is best to use L for clarity.
Examples of lowercase suffixes:
Lowercase suffix: long x = 10000l; // Is that 100001 or 10000l? Uppercase suffix: long x = 10000L; // It's 10000L.
Summary. Suffixes can be used on literal numbers. This is a way to tell the C# compiler that you want the literal number to be treated as a certain type of number, similar to a cast.
Home
© 2007-2022 sam allen.
see site info on the changelog.