Constants and Literals
In C# , you can declare constants for all data types. You have to initialize a constant constant at the time of its declaration. Constants are declared for value types rather than for reference types. To declare an identifier as a constant, the const keyword is used in the identifier declaration. The compiler can identify constants at the time of compilation because of the const keyword.
const <data type> <identifier name> = <value>;
const: keyword denoting that the identifier is declared as const.
Data type: Data type of constant.
Identifier name: Name of the identifier that will hold the constant.
Value: Fixed value that remains unchanged throughout the execution of the code.
The following code declares a constant, _pi , and a variable, radius, to calculate the area of the circle.
const float _pi = 3.14F;
float radius= 5;
float area = _pi * radius * radius;
Console.WriteLine (“Aread of the circle is “ +area);
In the above code, a constant called _pi is assigned the value 3.14 which is a fixed value. The variable, radius, stores the radius of the circle. The code calculates the area of the circle and displays it as the output.
A literal is a static value assigned to variables and constants. You can define literals for any data types of C#. Numeric literals might suffix which a letter of the alphabet to indicate the data type of the leteral. This letter can be either in upper or lower case. For example, in the following declaration, string bookName = “Csharp”, Csharp is a literal assigned to the variable bookName of type string.
In C#, there are six types of literals. These are:
- Boolean Literal: Boolean literals have two values, true or false. For example,
bool val = true;
where, true: Is a Boolean literal assigned to the variable val.
- Integer Literal: An integer literal can be assigned to int, uint, logn or ulong data types. Suffixes for integer literals include U, L, UL or LU. U denotes uint or ulong, L denotes long. UL and LU denote ulong. For example,
long val = 53L;
53L: Is an integer literal assigned to the variable val.
- Real Literal: A real literal is assigned to float, double (default), and decimal data types. This is indicated by the suffix letter appearing after the assigned value. A real literal can be suffixed by F, D or M. F denotes float, D denotes double and M denotes decimal. For example,
float val = 1.66F;
1.66F: Is a real literal assigned to the variable val.
- Character Literal: A character literal is assigned to a char data type. A character literal is always enclosed in single quotes. For example,
Char val = ‘A’;
A: Is a character literal assigned to the variable val.
- String Literal: There are two types of string literal in c#, regular and verbatim. A regular string literal is a standard string . A verbatim string literal is similar to a regular string literal but is prefixed by the @ character. A string literal is always enclosed in double quotes. For example,
string mailDomain = “@gmail.com”;
@gmail.com: Is a verbatim string literal.
Null Literal: The null lieteral has only one value, null. For example,
string email = null;
Null: specifies that email does not refer to any objects (reference).