Home
C#
Numeric Suffix Examples
This page was last reviewed on Nov 29, 2021.
Dot Net Perls
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, ulong
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.
Detail 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).
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.
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.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 29, 2021 (simplify).
Home
Changes
© 2007-2024 Sam Allen.