How to Use const Class Member Modifier in C# (C Sharp)What is a Constant? Constants are compile-time evaluated variables which has one time value assignment during declaration. Its value cannot be modified later. Class Member Variables can be defined as constants using the keyword const. Here is a simple example:
public class
testConst { The output of this program will be 100. Guidelines to be Followed While Using Constant: Do Not Change the Value of a Constant: You cannot modify a constant. A constant can never be on the left hand side of an expression. i.e., constants can never be part of any assignment statement. For example, public class
testConst { Here you try to change the value of a constant. Hence you will encounter the error CS0131 stating The left-hand side of an assignment must be a variable, property or indexer Initialize
Constant Variable at the Time of Declaration: You should initialize the
constant variable during its declaration itself. Consider the following
example: Here initialization is not done at the time of declaration. Hence you will end up in a compilation error CS0145 stating A const field requires a value to be provided You can Evaluate a Constant Value But Avoid Circular Definitions: Constants need not be directly assigned with values. You can also assign expressions to constants. But those expressions should in turn have only constants. For example, public class
testConst { This block of code is valid. However, both the above mentioned constants should not depend on one another. See the code below: public class
testConst { This scenario will end up in a compilation error CS0110 stating The evaluation of the constant value for testConst.constVar1 involves a circular definition Always Initialize Constant to Compile Time Value: Constants can be assigned with expressions. But these expressions have to be evaluated at compile time and not at run time. Consider the following example: public class
testConst { Here constObj will be initialized with the value only at run time. Hence this scenario will end up in the compilation error CS0133 stating The expression being assigned to testConst.constObj must be constant Do Not Mark Constant Variable as Static Explicitly: Constant variables are static in nature. Hence defining it as static explicitly will throw an error. For example, public class
testConst { This scenario will throw an error CS0504 stating The constant testConst.constVar1 cannot be marked static Only Specific Data Type Variables can be Declared as Constants: Class Member Variables of type sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, enum can only be declared as constants. In addition, constants can also be referenced to null value. Access Modifiers for Constants: Constants can have its access modifier as public or private or protected or internal or protected internal. You can Define Multiple Constants of Same Type in the Same Line: Consider the following example: public class
testConst { This block of code is valid and it means that both the constants are public variables of type int. Access Constant Outside its Class Using Class Name and Not its Instance: As already stated, constants are static class variables. Hence you can access it outside its class using class name as shown below: public class
testConst { This example assumes that both the classes belong to same package structure. If not, then ensure that the class name is fully qualified.
|