find and solve ||
Please wait.....

What is Constants in C# and how to use it.

As you already know, constants are placeholders that contain a single value throughout the execution of program.This implies that once you initialize a constant, you cannot change its value in the program.

Similar to variables,you need to first declare a constant before using it in the program.In C#,you need to use the const keyword to declare a constant. In addition, you need to specify the name, data type, and initial value of the constant. Note that it is necessary to initialize a constant when you declare it in the program; otherwise, an error is generated.

The syntax to declare a constant is as follows:

Const  <Data_Type>  <Contant_Name> = value;

In the prececeding syntax,

 const: Refers to the C# keyword used to declare a constant 
<Data_Type>:  Refers to the data type of the constant 
<Constant_Name>: Refers to the name of the constant 
=: Refers to the assignment operator 
value: Refers to the value assigned to the constant 

The following code snippet shows how to declare a constant: 

 const float PI = 3.14F;
 const int Planets = 9; 

In the preceding code snippet, two constants, PI and Planets, are declared by using the const keyword in two separate statements. The PI constant is of float data type and has the value 3.14. The Planets constant is of int data type and has the value 9. You cannot change the values of PI and Planets, as they are constants. 

You can also assign the value of one constant to another constant, as shown in the following code snippet: 

const int Length = 10; 
const int Breadth = Length; 

In the preceding Code snippet, Length is declared as a constant having a value, 10. The Length constant  is then assigned to the Breadth constant. This copies the value stored in the Length constant to the Breadth constant. 

 Note that you cannot use a variable to initialize a constant.For example, the following code snippet generates an error as Length is a variable and is assigned to the Breadth constant.

int Length 10; 
const int Breadth = Length; //will generate an error 

Some Character Constants Example

\\\ character
\'' character
\"" character
\?? character
\aAlert or bell
\fForm feed
\rCarriage return
\tHorizontal tab
\vVertical tab
\xhh . . .Hexadecimal number of one or more digits
For Example

using System;
namespace EscapeCharDemo {
   class Program {
      static void Main(string[] args) {
Hello   FindAndSolve

The following program demonstrates defining constant  and implated constant

using System;
namespace ConstantsDemo {
   class Program {
      static void Main(string[] args) {
         const double pi = 2.14159;               
         // constant declaration 
         double r;
         Console.WriteLine("Enter Radius: ");
         r = Convert.ToDouble(Console.ReadLine());            
         double areaCircle = pi * r * r;
         Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.


Report Response