Topic : Constant | Literals | Escape Sequence | In C# Programming Language:

What is Constant:

Like a Variable, Constant refer to fixed values that the Program may not change the values during its execution. This fixed values is also known as Literals. Constants can be any of the basic data types like an Integer constant, A Floating constant or a character constant. The Constant are treated just like regular variables, except that their values can't be modified after their definition.



Defining Constants:

We can define the constants using the const Keyword. The Syntax is:


const < Data_type > <Constant_name>  =  18; 


Let's have an example to know, how this const keyword work.



/*Example: Constant In C#: InfoBrother: */

using System;

namespace Constant
{
    class Program
    {
        static void Main(string[] args)
        {

            const double PI = 3.14159;  //constant declare:

            double rad;

            Console.WriteLine("Enter the Radius: ");
            rad = Convert.ToDouble(Console.ReadLine()); //convert double to string format.

            double Area_Circle = PI * rad * rad;   //Formula : Area = PI *R^2.
            Console.WriteLine("The Area of Circle is: " + Area_Circle);


            Console.ReadKey();
              
        }
    }
}


Character Constants:

Character Literals are single Unicode character and enclosed in single Quotes. For Example, 'x' and can be stored in a Simple variable of char type. A Character literal can be plain character like 'x' and escape Sequence like '\t', or a universal character such as '\u02c0'.

There are certain characters in C# when they are preceded by a Backslash. they have special meaning and they are used to represent like newline (\n) or tab (\t). Here is a list of some escape sequence codes:



Escape Sequence Meaning
\\Use to Represent ( \ ) Backslash character.
\'Use to Represent ( ' ) Single Quote.
\"Use to Represent ( " ) Double Quote.
\?use to Represent ( ? ) Question Mark.
\aUse to get Alert or Bell.
\bUse to get Backspace.
\fUse to get Form Feed.
\nUse to get Newline.
\rUse to get Carriage Return.
\tUse to get Horizontal Tab.
\vUse to get Vertical Tab.
\0Use to Represent Null
\xhxxxxUse to Represent Hexadecimal number of one or more digits.
\uxxxxUse to represent Character corresponding to its Unicode number.


Integer Literals:

Integer Literal can Store Numeric value. They can be decimal, octal or hexadecimal type. They can also have a sign, prefix or suffix. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be Uppercase or lowercase and can be in any order.

/*Example: */

85         // decimal
0x4b       // hexadecimal
30         // int 
30u        // unsigned int
30l        // long 
30ul       // unsigned long 


Floating Point Literals.

A Floating point literal has an integer part, a decimal point, fractional part, and an exponent part. We can represent floating point literals either in decimal form or exponential form.

/*Example: */

3.14159       // Legal 
314159E-5F    // Legal 
510E          // Illegal: incomplete exponent 
210f          // Illegal: no decimal or exponent 
.e55          // Illegal: missing integer or fraction


Boolean Literal:

Boolean Literal can store two values, true and False. Boolean data type is used to store these values.

/*Example: */

bool condition  = true; 


String Literal:

String Literals are set of character enclosed between double Quotes ( "" ) or ( @"" ). They can store characters or escape sequences. Strings initialized using ( @"" ) are known as verbatim string. Escape sequences does not work in verbatim string. Lines can be broke down to small lines by just using blank space.

/*Example: */

"string literal"    //Output: string literal
@"string literal"   //Output: string literal
"string \t literal" //Output: string      literal
"string             //string 
literal"            //literal
""Hi""              //"Hi" 


Null Literal:

Null Literal denotes Null type. We can use it to denote that nothing is referenced to the NULL constant or variable.


int a = null;
if (a == null)
      {
        Console.WriteLine("null value");
      }


  • Some of the Best Practices while Using constants:

    » Constants need to be initialized during declaration.
    » Constants are to be used with meaningful names as they represent special values.
    » To define multiple Non-integral/integral constants, a single static class (containing constant member variables) can be used to group them.
    » The Scope of the constant variable is limited to a single assembly, class, or method.















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement