Variable as constant

  • Post author:
  • Post category:csharp-category
  • Reading time:7 mins read

Variable as constant

The constants are the fixed type of fields (variable) whose values can never be changed. The value to a constant variable assigned at compile time. In C and C++ we could use preprocessor directives such as #define to define constant variables, but in C# we cannot use preprocessor directives to define constant variables. The syntax of defining a constant variable is this as shown below.

     <const keyword> <data type> <variable name> = <value>;

Declaration and definition of a constant variable are done at the same time, otherwise, it will be a compile-time error. For example,

   const int magicNumber = 5;

A literal meaning is a value that is hard-coded into your source code. So we assign those literals to constant variables because which we don’t want to change at any time in our program. There are six types of literals.

  • Integer Literals
  • Boolean Literals
  • Character Literals
  • String Literals
  • Real Literals
  • Null Literals

 

Integer Literals

These literals are used to write integer type values for the integer type of variables (int, long, short, byte, short, ulong). These literals can have two possible types: decimals & hexadecimal. For example.

100 //decimal

0x123f //hexadecimal

072 //octal

1000 //integer

100u //uint

1000 //long

10ul //along

Boolean Literals

There are two types of Boolean literals: true or false. For example.

   bool isLarge = true;

Character Literals

We use character literals to represent a single character. A character literal is always represented in a single quotation mark. For Example.

   char firstCharacter = 'a';

String Literals

C# supports two types of string literals.

  • Regular String Literals
  • Verbatim String Literals

A Regular String Literal consists of zero or more characters enclosed in double quotation marks. For example.

   string Country = "United States Of America";

   string emptyString = "";

A  Verbatim String Literal consists of an @ character followed by zero or more character in double quotation marks. For example.

   string Country = @"United States Of America";

Real Literals

Real Literals are used to write the values of floating-point type variables (float, double, decimal). For Example.

10.15 //double

100.72f //float

1.45d //double

1.44m //decimal

e23 //exponent. Means 1023

Null Literals

Null literals are used to represent the Null types. They can be fit into any reference-type. For example.

   int x = null;
   Console.WriteLine(x);

The output will be null.

The value of x is null. So when we don’t have any particular value for a variable we assign it a null value and latter we can use this variable whenever we have a specific value for it.

Now let’s come to our main topic which is Constant Variables. Below is a given full working example of how to use constant variables in the C# program.

   using System;
   namespace Zitoc
   {
      class ConstantVaribals
      {
         public static void Main(string [] args)
         {
           const int max = 100;
           //if you assign max an other value latter 
           //it will be a compile time error
           // max=20;
           Console.WriteLine(max); //output 100 on console screen
         }
      }
   }

Constants are useful when you want to use a single value throughout your program when needed. Using constants instead of values each time makes your program more readable. For example, if you want to use a value of PI (3.14) in your program and every time you want to use you write its value, typing constant values, again and again, is very difficult if you have more than one constant values. So instead of writing, hard-coded values use constant variables to store the constant values such as the value of PI (3.14).

   const int PI = 3.14;

It is important to note that, unlike variables, constant variables must always be initialized at the point when it is declared.

Variable and data types

admin

We are a team of writers, researchers, and editors who are passionate about helping others live their best lives. We believe that life is a beautiful gift. We try to live our lives to the fullest and enjoy every moment. We are always learning and growing, and we cherish the relationships we have with our family and friends.

Leave a Reply