• Have Any Question?
  • contact@eitworld.com

csharp.net constants

ul class=”points”>

  • Like variables constants are also used to store the data values but the values stored in the constants are fixed that cannot be altered at the time of execution of program.
  • These fixed values assigned to a constant are known as the ‘Literals’.
  • Literals in C#.Net can be classified into the following categories:
    • Boolean Literals
    • Integer Literals
    • Floating Point Literals
    • Character Literals
    • String Literals
    • The null Literals

Here we are going to explain each type of literal in brief:

Boolean Literal
    • Boolean data type can contains only two literal values that are ‘True’ and ‘False’. No other value can be assigned to Boolean data type.
    • This is an example of Boolean Literal:
bool IsValid = True;
bool IsRight = False;

Integer Literal
    • Integer Literals can be specified as the number without any fractions.
    • Integer Literal can be of type int, uint, long or ulong.
    • To specify the uint, long and ulong we used suffix as u/U, l/L and ul/UL respectively, No suffix is used for the int .
    • An integer literal can be hexadecimal, octal or decimal.
    • To specify hexadecimal and octal integer literals we used prefix as 0x/0X and o/O respectively. No prefix is used for the decimal.
    • These are some examples of Integer Literals:
95 // decimal
0xAB10 // hexadecimal
076 // octal
12u // unsigned int
32ul // unsigned long
45L // long
39 // int

Floating Point Literals
    • Floating-point literals require the use of the decimal point followed by the number’s fractional component.
    • Floating Point Literals can be of type floats and double.
    • To specify the float literal suffix f/F is used and for specify the double literal suffix d/D is used.
    • C#.Net also allows users to use scientific notation for floating-point numbers for this exponent part is also added in the floating point number.
    • Here are some examples of the Floating Point Literals:
3.14f // float value
3.123456d // double value
314.e54 // exponent floating value

Character Literal
    • Character literals are enclosed in the single quotes. Like ‘a’ or ‘C’.
    • Character literal can be a simple character or a escape sequence.
    • Escape sequences are the special characters which have some meaning and started with the backslash. Following table contains the various character escape sequences with their meanings:


String Literals
    • A string literal is a set of characters enclosed by double quotes.
    • A string literal have also a format like @””.
    • Here are some examples of String Literal:
“Welcome to Home”
“Priya Sharma”
@”Welcome to Home”

null Literals
  • A null literal contains a null value.
  • The type of a null-literal is the null type.

Declaration of a constant

    • You have to declare the constant before using it into the program.
    • 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 a same time otherwise an error will occur.
    • Here is the syntax for the declaration of the constant:
const datatype constantname = value;

    • Here is an example of the constant:
const float pi = 3.14f;
const int length= 10;

Real Time Web Analytics