C# const (Constant) Keyword

In c#, const is a keyword which is used to declare a constant fields in our applications. Generally, in c# the constant field values are set at compile time and those values never be changed.

 

In c#, if we use const keyword to declare constant field, then that field value cannot be changed throughout the application so we should not use const keyword with the fields whose value will be changed at any time.

 

To define a constant fields in c#, we need to use const keyword during the declaration of fields in our application and we can use constants with numbers, boolean values, strings or with null references.

 

It’s mandatory to initialize a constant fields with required values during the declaration itself otherwise we will get a compile time errors in our c# application.

 

In c#, the static modifier is not allowed to use during the declaration of constant variables.

C# Constant Syntax

Following is the syntax of defining a constant fields using const keyword in c# programming language.

 

const data_type field_name = "value";

If you observe above syntax, we used a const keyword declare a constant variable in our application.

 

Following are the different ways of declaring and initializing a constant variables in c# programming language.

 

// Constant variables

const string name = "Suresh Dasari";

const string location = "Hyderabad";

const int age = 32;

If you observe above examples, we created a constant fields with different data types based on our requirements.

C# Constant Example

Following is the example of defining and using constant fields in c# programming language with const keyword.

 

 using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            // Constant variables

            const string name = "Suresh Dasari";

            const string location = "Hyderabad";

            const int age = 32;

            // This will throw compile time error

            //name = "Rohini Alavala";

            Console.WriteLine("Name: {0}", name);

            Console.WriteLine("Location: {0}", location);

            Console.WriteLine("Age: {0}", age);

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we created a constant fields with different data types and if we uncomment the commented code, then we will get a compile time error because we are trying to change the value of constant fields.

 

As discussed, once the constant field is declare and initialized, then that field value must be same throughout the application.

 

When we run above c# program, we will get the result like as shown below.

 

C# Constant Keyword Example Result

C# Constant Features

Following are the important features of constant variable in c# programming language.

 

  • Constant fields in c# can be created by using const keyword.
  • In c#, the constant fields must be initialized during the time of declaration.
  • Constant field values will be evaluated during the compile time in c#.
  • Once values assigned to a constant fields, then those values must be a same throughout the application.

This is how we can use constant fields in our c# programming language based on our requirements. 

Topics Covered