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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
               
        }
    }
}

chevron_right


Output:

A: 15
B: 50.83

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
   
        }
    }
}

chevron_right


Output:

C: S
D: 70.23
E: Geeks



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.