To understand constants and their usage in C#, dive into the article below by Mytour for more insights.
Constants and Their Application in C#
2. Understanding Literals in C#
Literals in C# are the essence of values. As seen in the example above, 3.14159, ' character', and 5 are all literals.
Here are some types of literals in C#:
2.1. Boolean Literals
Boolean Literals can store two values: True and False. The Bool data type is used to hold these values.
Here's an example of Boolean Literals:
bool condition = true;
2.2. Integer Literals
Integer Literals can store numeric values, which can be in base 10 (decimal), base 8 (octal), or base 16 (hexadecimal). They can also include a sign, prefix, or suffix.
- Use + or - to denote integers.
- Use a prefix to indicate the format of the integer. Use '0x' or '0X' for base 16, '0' for base 8. If no prefix is used, it defaults to base 10.
- Use a suffix 'u' - 'U' or 'l' - 'L' to represent the integer type. 'l' or 'L' denotes a long integer, 'u' or 'U' denotes an unsigned integer. If no suffix is used, it defaults to int.
Here's an example of Integer Literals:
55 //decimal
0x125f //hexadecimal
056 //octal
10 //integer
10u //uint
10l //long
10ul //ulong
2.3. Real Literals
Real Literals store numeric values. These values can be floating-point types, include a sign, suffix, or be in decimal format. They can also be in exponent form.
- Use + or - to denote floating-point numbers.
- Use suffix 'f' - 'F' or 'd' - 'D' or 'm' - 'M' to represent the real literal type. 'f' or 'F' for float, 'd' or 'D' for double, and 'm' or 'M' for decimal. If no suffix is used, it defaults to double.
- 'e' can be used for exponent types.
Here's an example of Real Literals:
1.23 //double
1.23f //float
1.23d //double
1.23m //decimal
e23 //exponent. Means 1023
2.4. Character Literals
Character Literals are single Unicode characters enclosed in single quotes. Values stored in character literals include characters (e.g., 'a'), character codes (e.g., '\u0097'), and escape sequences, represented by the char data type.
Escape sequences are not used directly. Here's a list of some escape sequences:
\\ - used to retrieve the character '\'.
\' - used to retrieve a single quote.
\' - used to retrieve a double quote.
\n - newline.
\r - carriage return.
- backspace.
\t - horizontal tab.
\a - alert.
\f - form feed.
\v - vertical tab.
\0 - null.
\uXXXX - character corresponding to Unicode number.
2.5. String Literals
String Literals are collections of characters enclosed in double quotes '' or @'', which can include characters or escape sequences. Strings initialized using @'' are called verbatim strings. Escape sequences do not work within verbatim strings.
You can break lines into smaller lines using spaces.
Here's an example of String Literals:
'string literal' //Output: string literal
@'string literal' //Output: string literal
'string \t literal' //Output: string literal
'string //string
literal' //literal
''Hi'' //'Hi'
2.6. Null Literals
Null Literals are used to represent nothing being referenced to a null constant or variable.
Here's an example of Null Literal:
int a = null;
if (a == null)
Console.WriteLine('null value');
/*output:
null value*/
This article by Mytour introduces you to constants and how to use them in C#. Additionally, readers can explore other articles on Mytour to gain a deeper understanding ofOperators in C#.
