C# Constants and literals are used to store the data values. Neverthless, the values stored in the constants are fixed and cannot be altered at the time of the execution of the program. Constants can be of any basic data type like an integer constant, a floating constant, a character constant, or a string literal.
Const, a keyword, indicates a constant. It describes an entity that cannot be changed at program runtime. Instead, the entity must be fully resolved at compile-time. A constant cannot be reassigned.
Literal in C# is a source code representation of a value.
integer-literal
real-literal
character-literal
string-literal
1. Integer Literal
Description
Integer Literal can be specified as the number without any fractions. It can be of type int, uint, long or ulong. To specify uint, long and ulong with the suffixes u/U, l/L and ul/UL respectively. No suffix is used for int.
An integer literal can be hexadecimal, octal or decimal. To specify hexadecimal and octal integer literals prefixes 0x/0X and o/O are used respectively. No prefix is used for decimal.
Examples of Integer Literals
95 // decimal
0xAB10 // hexadecimal
076 // octal
12u // unsigned int
32ul // unsigned long
45L // long
39 // int
2. Floating Point Literals
Description
Floating-point literal requires the use of the decimal point followed by the number's fractional component. It can be of type float and double.To specify the float literal, suffix f/F is used and to specify the double literal, suffix d/D is used. C# also allows users to use scientific notation for floating-point numbers for which the exponent part is also added in the floating point number.
Examples of Floating Point Literals
3.14f // float value
3.123456d // double value
314.e54 // exponent floating value
3. Character Literal
Description
Character literal is enclosed in single quotes. Like 'a' or 'C'. It can be a simple character or a escape sequence. Escape sequences are the special characters which have some meaning and begin with a backslash.
Examples of Character Literal
Charecter
Meaning
\a
Alert
\b
Backspace
\f
Form Feed
\r
Carrige Return
\'
Single Quote
4. String Literal
Description
String literal is a set of characters enclosed by double quotes. A string literal also has a format like @" ".
Examples of Character Literal
"Welcome to SPLessons"
@"Have a nice day"
5. Null Literal
Description
A null literal contains a null value. The type of a null-literal is the null type.
Declaration:
It can be declared with a meaningful name of a specific data type and also with a fixed value which cannot be altered at the time of execution.You have to declare and initialize a constant at the same time to avoid an occurrence of an error.
Examples of Null Literal
const float pi = 3.14f;
const int length= 10;
Examples
The example provided below shows a simple program.
[csharp]
using System;
namespace SPLessons
{
class Program
{
const string obj = "Welcome to SPlessons";
static void Main()
{
Console.WriteLine(obj);
Console.WriteLine(Program.obj);
const string txt = ".txt";
Console.WriteLine("Download file here" + txt);
}
}
}
[/csharp]
Output: