C# const (Constant) Keyword

In c#, const is a keyword, and it is helpful to declare constant fields in our applications. Generally, in c#, constant field values are set at compile-time, and those values will never change.

 

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

 

To define 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 null references.

 

It’s mandatory to initialize constant fields with required values during the declaration itself; otherwise, we will get 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 constant fields using const keyword in c# programming language.

 

const data_type field_name = "value";

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

 

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

 

// Constant variables
const string name = "Suresh Dasari";
const string location = "Hyderabad";
const int age = 32;

If you observe the above examples, we created 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 the above example, we created constant fields with different data types, and if we uncomment the commented code, 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 declared and initialized, the field value must be the same throughout the application.

 

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

 

C# Constant Keyword Example Result

C# Constant Features

The following are the important features of a constant variable in the c# programming language.

 

  • In c#, we can create the constant fields using const keyword.
  • In c#, the constant fields must initialize during the time of declaration.
  • In c#, the constant field values will evaluate during the compile time.
  • Once values are assigned to constant fields, those values must be the same throughout the application.

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