Appendix D Constant Declarations
Constant declarations are similar to the variable declarations described in Appendix C, “Variable Declarations.” The main differences are you must include an initialization statement to give the constant a value and you cannot change the value after the constant is initialized.
You may also need to use a literal type character to explicitly give a value’s type. For example, the following code attempts to create a float constant and set its value to 5.8. Unfortunately, C# interprets the literal string 5.8 as a double and not a float, so Visual Studio flags this as an error.
const float taxRate = 5.8;
To avoid this problem, or to make your code more explicit, you can follow a literal value with a literal type character to indicate the value’s data type, as in the following code.
const float taxRate = 5.8F;
The following table lists C#’s literal type characters.
| Character | Data Type |
| U | uint |
| L | long |
| UL, LU | ulong |
| F | float |
| D | double |
| M | decimal |
You can use either uppercase or lowercase for literal type characters.
You can also precede an integer literal with 0x or 0X to indicate that it is a hexadecimal value.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access