Literals and Constants - C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

Chapter 6. Literals and Constants

The term“literals” refers to fixed or unchangeable values: you cannot alter these values while the program is being executed. Literals are also known as constants. Literals can take the form of any basic data type (e.g. floating constant, integer constant, string constant, character constant, etc.). Additionally, you can use enumeration literals in your C# statements.

The Integer Constants

Integer constants can be octal, decimal, or hexadecimal literals. You should use a prefix to specify the base (also known as radix). Check this list:

· Use 0 for octal literals

· Use oX or ox for hexadecimal literals

· Decimal literals don’t require prefixes

Integer literals may also have L and U as a suffix. L is used for long integers while U is used for unsigned integers. These suffixes are not case-sensitive.

The list below will show you valid and invalid integer constants:

· 131– Valid

· 312u– Valid

· 0XYoU– Valid

· 218– Invalid: The number 8 isn’t octal.

· 123UU– Invalid: You must not repeat suffixes.

Here’s a second list of examples. This one, however, will show you the different kinds of Integer Constants:

· 0123– octal

· 86– decimal

· 0x5b– hexadecimal

· 20– int

· 20u– unsigned integer

· 20l– long integer

· 20ul– long and unsigned integer

The Floating Point Constants

Floating point constants may have the following parts:

1. a decimal point

2. an exponent

3. an integer

4. a fraction

Here are valid and invalid samples of floating point constants:

· 3.14– Valid

· 314-5L– Valid

· 310E– Invalid: The exponent is incomplete.

· 310f– Invalid: This constant doesn’t have an exponent or decimal.

· .e44– Invalid– This constant has a missing fraction or integer.

While using decimal numbers, you should include the exponent, the decimal point, or both. While you are using exponential numbers, however, you should include the fraction, the integer, or both. You should introduce signed exponents using“E” or“e.”

The Character Constants

You should place character constants inside single quotes. For instance, you can store‘a’ inside a plain char type variable. In general, character constants can take the form of simple characters (e.g.‘b’), universal characters (e.g.‘\u03B0’) or escape sequences (e.g.‘\t’).

In the C# language, some characters gain a special meaning if they are introduced by a backslash. Since they have a special meaning, you can use them for certain functions such as tab (\t) or newline (\n). The following list will show you some escape sequences and their meanings:

· \’ – This represents a single quote character.

· \” – This represents double quote characters.

· \\ - This represents a backslash character.

· \?– This represents a question mark.

· \a– This represents a bell or an alert.

· \b– This represents a backspace.

· \n– This represents a newline.

· \f– This represents a form feed.

· \t– This represents a horizontal tab.

· \r– This represents a carriage return.

· \ooo– This represents an octal number that has 1-3 digits.

· \v– This represents a vertical tab.

· \xhh… - This represents a hexadecimal number that has one or more digits.

The String Constants

You should use double quotes (i.e.“” or @””) to enclose string constants. In general, string constants are similar to character constants: they contain escape sequences, universal characters, and plain characters.

You may use a string literal to break long lines into smaller ones. Then, you may use whitespaces to separate the small lines. The following list will show you some string constants:

· “hi, girl”

· “hi, \

girl”

· “hi,” “g” “irl”

· @“hi girl”

Important Note: The string constants given above are identical. They will give the same result: hi girl

How to Define Literals

You can use const (i.e. a C# keyword) to define constants. When defining constants, you should use the following syntax: