Constants in C#

By Sep 28, 2018

Description:

Here we explain how to declare constants in C# and what the constants are used for in a program.

Preferencesoft

A constant is an identifier that refers to a fixed value.

A constant has in its declaration all aspects of a variable. However, it is impossible to dynamically change its initial value.

Usefulness of a constant

When the same value for example 0.123456789 appears several times in an instruction block, it becomes interesting to declare this value constant and to replace all occurences of the value by the name of the constant. This ensures compiling the reservation of a single location for the value 0.123456789 in the program's data area and a better clarity of the program. Another advantage for the developer is that of being able to modify the value of the constant quite easily. For example, if in a program the value 3.14 appears ten times and if the programmer wants to replace it by 3.141 then by 3.1415926 it will be able to save time to declare a constant pi.

Declaration of a constant

To declare a constant, we write the keyword const followed by the type T of the constant, the identifier c of the constant, the symbol = and the literal value of the constant, ended by a semicolon:

const T c = 12;

const <data_type> <constant_name> = <value_litteral>;

It is possible to declare constants whose value is computed by arithmetic or logical operations from literal values or other constants.

Thus, one can define a constant from several others. But the constants being typed, it is necessary in some cases to convert a datum of one type to another by means of a cast.

The ternary conditional operator can also be used to assign an initial value according to a given Boolean condition.

Note that the operations allowed for constants are limited. For example, you cannot define the constant c1 as follows:

const double c1 = Math.Cos(1);

In fact, the Cos method of the class Math will not be executed during compilation.

Constant Type

Integer constant

const int i = 78;
const int ib = 0b01111111000000001111111100000000;
const int ibm = unchecked((int)0b11111111111111111111111111111111);
const int ibopo = -(int)0b01111111111111111111111111111111;
const int ir = (int)78.5555; //explicit cast
const int ic = 'A'; //implicit cast
const int ihex = 0x000000FF;
const int ihexn = unchecked((int)0xFFFFFFFE);
const int io = 1212 - 7879878 + (45 & 46456);
const int ip = 1212 - 7879878 / 3;

Long constant

const long l1 = -1L;
const long l2 = 12; //implicit cast
const long ls = -1111111111111L + 4556;
const long lb = 0b0000000011111111000000001111111100000000111111110000000011111111; // 64 bits
const long lhex = 0x7F00FF00FF00FF00;
const long lhexn = unchecked((long)0xFF00FF00FF00FF00);
const long lp = 456 * 897 * 545;

Double constant

const double r = Math.E;
const double r1 = -1; //implicit cast
const double epsilon = 1e-14;
const double dep1 = Math.E + 1;
const double sqrt2 = 1.414;

String constant

const string s = "Hello";
const string sc = "Hello " + "John"; //Concatenation
const string sd = "\u0030"; //0

Unsigned integer constant

const uint ui = 45;
const uint ui1 = 65456u;
const uint ui2 = 0xFFFFFFFF;
const uint ui3 = 0xFFFF0000 + 65535;
const uint ui4 = 0xFFFF0000 + 65535;

Unsigned Long constant

const ulong ul1 = 56UL;
const ulong ul2 = 50006L; //automatic cast
const ulong uls = 123456UL + 789456123UL; //automatic cast
const ulong ult = (ul1==4654?5646:78978);

Byte constant

const byte b = 12;
const byte by = 0b11111111;
const byte bc = (byte)'f';
const byte bo = b + bc;
const byte bop = 54 | 87;

Signed Byte constant

const sbyte b = -12;
const sbyte by = unchecked((sbyte)0b11111111);
const sbyte bc = (sbyte)'f';
const sbyte bp = bc + 10;
const sbyte bo = b + bc;
const sbyte bh = 0x7F;
const sbyte bhn = unchecked((sbyte)(0x7F + 1));
const sbyte bop = 54 | 87;
		

Float constant

const float f = 0.123F;
const float fs = 0.123f + 45.89e-1f - 2.2f;
const float fi = 10; //implicit cast
const float fd = (float)123.567; //explicit cast
const float pi = (float)Math.PI;
const float twoPi = pi * 2;

Char constant

const char c = '4';

CSharp

Categories

Share

Follow


KodFor Privacy Policy