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 {
const int constVar = 100;
public static void Main( ) {
System.Console.WriteLine(constVar);
}
}

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 {
const int constVar = 100;
public static void Main() {
constVar = 500;
}
}

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:
public class testConst{
const int constVar;
public static void Main() {
constVar = 500;
}
}

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 {
const int constVar1 = 100;
const int constVar2 = constVar1 + 45;
public static void Main() {
System.Console.WriteLine(constVar1, constVar2);
}
}

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 {
const int constVar1 = 100 + constVar2;
const int constVar2 = constVar1 + 45;
public static void Main(){
System.Console.WriteLine(constVar1, constVar2);
}
}

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 {
public const testConst constObj = new testConst();
public static void Main(){ }
}

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 {
static const int constVar = 100;
public static void Main() {
System.Console.WriteLine(constVar);
}
}

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 {
public const int constVar1 = 100, constVar2 =200;
public static void Main() {
System.Console.WriteLine(constVar1, constVar2);
}
}

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 {
const int constVar = 100;
public static void Main(){
System.Console.WriteLine(constVar);
}
}
public class useConst {
public static void Main() {
System.Console.WriteLine(testConst.constVar);
}
}

This example assumes that both the classes belong to same package structure. If not, then ensure that the class name is fully qualified.

|How and Why to Use Trace Option for Debugging in ASP.NET Application | How to Display Tool Tip in Your ASP.NET Web Application | How to Use “const” Class Member Modifier in C# | How to Use HTML Help Viewer in Your ASP.NET Web Application | List of Preprocessor Directives Available in C# | More about #define, #undef and Conditional Compilation Directives in C# |Understanding Different Class Member Modifiers in C# | Understanding of Diagnostic Directives and #pragma, #line, #region, #endregion Directives in C# | Usage of Roles API to Perform Authorization in .NET| What are the Different Compiler Engineering Techniques|Writing Unsafe Code in C#|

 


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.