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:
using System;
using System.Text;
namespace Geeks
{
class GFG
{
static void Main( string [] args)
{
const int A = 15;
const float B = 50.83f;
Console.WriteLine( "A: {0}" , A);
Console.WriteLine( "B: {0}" , B);
}
}
}
|
Output:
A: 15
B: 50.83
Example 2:
using System;
using System.Text;
namespace Geeks
{
class GFG
{
static void Main( string [] args)
{
const char C = 'S' ;
const double D = 70.23;
const string E = "Geeks" ;
Console.WriteLine( "C: {0}" , C);
Console.WriteLine( "D: {0}" , D);
Console.WriteLine( "E: {0}" , E);
}
}
}
|
Output:
C: S
D: 70.23
E: Geeks
Last Updated :
22 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...