Open In App

Program to define various types of constants in C#

As in other programming languages, various types of constants are defined the same as defined in C#, we can also define various types of constants and print their values. They are fixed in values in the program. There can be any types of constants like integer, character constants, float, double, string, octal, hexadecimal, etc.

Define constant: By using const keyword a constant can be defined. Its value can never be changed once it is defined.



Syntax:

const data_type constant_name = value;

Example:



Input: const int a = 15;  
Output: a: 15

Example 1:




// C# program to define different types of constants 
using System;
using System.Text;
   
namespace Geeks
{
    class GFG
    {
        // Main Method 
        static void Main(string[] args)
        {
            // integer constant
            const int A = 15;  
              
            // float constant
            const float B = 50.83f; 
           
            // to print above values
            Console.WriteLine("A: {0}", A);
            Console.WriteLine("B: {0}", B);
               
        }
    }
}

Output:

A: 15
B: 50.83

Example 2:




// C# program to define different types of constants 
using System;
using System.Text;
   
namespace Geeks
{
    class GFG
    {
        // Main Method 
        static void Main(string[] args)
        {
            // character constant 
            const char C = 'S';
              
            // double constant
            const double D = 70.23;
              
            // string constant
            const string E = "Geeks";   
   
            // to print above values
            Console.WriteLine("C: {0}", C);
            Console.WriteLine("D: {0}", D);
            Console.WriteLine("E: {0}", E);
   
        }
    }
}

Output:

C: S
D: 70.23
E: Geeks

Article Tags :