C# Constants are immutable values that are known at compile time and remain constant throughout the program’s existence. The const modifier is used to declare constants. Const declarations are only allowed for C# built-in types (excluding System.Object). Const cannot be used to user-defined types such as classes, structs, and arrays. Use the readonly modifier to make a class, struct, or array that is only initialised once at runtime (for example, in a function Object() { [native code] }) and cannot be altered after that.
Const methods, properties, and events are not supported in C#.
For integral built-in types, the enum type allows you to construct named constants (for example int, uint, long, and so on). See enum for further information.
C# Constants are the fixed values that can’t be changed during their execution in the program. Constants can be of any basic data type like integer, floating, character, or string constants. These values can not be modified after their definition.
Defining C# Constants:
The const keyword is used to define Constants.
Syntax:
const <data_type> <constant_name> = value;
Multiple constants of the same type can be declared at the same time, for example:C#Copy
class Calendar2 { public const int Months = 12, Weeks = 52, Days = 365; }
The expression that is used to initialize a constant can refer to another constant if it does not create a circular reference. For example:C#Copy
class Calendar3 { public const int Months = 12; public const int Weeks = 52; public const int Days = 365; public const double DaysPerWeek = (double) Days / (double) Weeks; public const double DaysPerMonth = (double) Days / (double) Months; }
Public, private, protected, internal, protected internal, and private protected are all possibilities for constants. These access modifiers specify how the constant can be accessed by class members.
Because the value of a constant is the same for all instances of the type, it is accessed as if it were a static field. You don’t declare them with the static keyword. To access the constant, expressions that are not in the class that defines the constant must use the class name, a period, and the name of the constant. Consider the following scenario:
using System; namespace ConstantDeclaration { public class Program { public static void Main(string[] args) { const double pi = 3.14159; double r; Console.WriteLine("Radius Value: "); r = Convert.ToDouble(Console.ReadLine()); double areaCircle = pi * r * r; Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle); } } }
Integer Constants:
An Integer Constants can be a decimal or hexadecimal constant. A prefix specifies the base for hexadecimal, There is no prefix id for decimal.
Examples:
Following are some examples of integer constants.
122 /* Legal */ 95 /* decimal */ 20 /* int */ 30u /* unsigned int */ 20l /* long */ 20ul /* unsigned long */
Floating-point Constants:
A floating-point constant has an integer, decimal, fractional, and exponent part. Floating-point constants can be represented either in decimal form or exponential form.
Examples:
Following are some examples of floating-point constants.
3.14159 /* Legal */ 314159E-5F /* Legal */ 510E /* Illegal: incomplete exponent */ 210f /* Illegal: no decimal or exponent */ .e55 /* Illegal: missing integer or fraction */
String Constants
String constants are enclosed in double-quotes (” “) or with @””. String constants contain plain characters, escape sequences, and universal characters. A long string can be divided into multiple lines.
Examples:
Following are some examples of string constants.
"Programming" "Csharp, \ Tutrorial" "Csharp, " "c" @"Programming"
Character Constants
Character Constants are always enclosed in single quotes. A character constant can be a plain character such as ‘x’, an escape sequence such as ‘\t’, or a universal character such as ‘\u02C0’.
Escape sequences | Description |
---|---|
\a | Alert or bell |
\b | Backspace |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\f | Form feed |
\v | Vertical tab |
\\ | \ character |
\’ | ‘ character |
\” | ” character |
\? | ? character |
\xhh . . . | Hexadecimal number of one or more digits |
using System; namespace EscapeChar { public class Program { public static void Main(string[] args) { Console.WriteLine("CSHARP\tTUTORIAL\n\n"); Console.ReadLine(); } } }