Constant Field in C# Programming Language
Any variable which is declared with a “const” keyword is called a constant field. In the constant field, only the data that is never to be changed is stored because the value of the constant field cannot be altered or deleted through the program’s life, so it must be consistent throughout. We also have to keep in mind that the C# programming language does not allow methods, events, and properties to be declared as a constant field, so we will be only able to classify variables of different data types in the C# programming language.
Now we will discuss how to initialize a constant field in a C# program.
As we can see, we used the “const” keyword before giving the data type to the variable. This clearly shows that this variable is no longer simple and cannot be referred to as a variable because it is a constant field now. Its value is constant throughout the lifespan of the program.
Now we will implement this concept and analyze several scenarios in the Ubuntu 20.04 environment.
Example 01: Making an Integer Variable a Constant Field in a C# Program in Ubuntu 20.04
In this example, we will be initiating an integer variable and declaring it as a constant field while assigning a value to it. As we know, the “const” keyword would be used before stating the variable’s data type, and even the value would be assigned after that.
As we can see in the above C# program, we have declared a constant field of the integer data type. We have built a basic main function in which we will first use the “const” keyword to create a constant field of the integer type, call it “Hours of a Day,” and set its value to “24.” as we know that this is a universal constant as we have 24 hours in a day. So, we can use this as a constant value throughout the program. Now we will write this with the help of Console.WriteLine() function and see if it is allowed.
As we can see that the correct value of the constant field is shown in the output, it proves that we can pass a constant field of integer data type in a function’s parameter.
Example 02: Making a String Variable a Constant Field in a C# Program in Ubuntu 20.04
Now we will be focusing on the string data type constant field. This example will classify a string variable as a constant field using the constant “keyword.”
We have specified a constant field of the string data type in the above C# program, as can be seen. We have developed a simple main function in which we will use the “const” keyword to create a string-type constant field called “str” with the value “Hello”. We will now write this constant field using the WriteLine() method and verify if it is permitted for a constant field of string data type to be passed in a method.
As the output displays the string data type constant field’s values correctly, it proves that we can pass this constant field in a method as well.
Example 03: Making a String and Integer Variable a Constant Field in a C# Program in Ubuntu 20.04
In this example, we will be discussing the method of making a string and integer variable a constant field in the C# programming language. The versatility of the constant field will be assessed in this example as we will use multiple constant fields in a single program and look at how they will react when they are passed in a method.
In this C# program, we will be creating two constant fields of different data types. First, we will make an integer data type constant field with the label “Hours of a day” and a value of “24,” and then a string data type constant field with the label “str” and a value of “Hello.” After this, we will pass both these constant fields in the Console.WriteLine() method and see how they will be displayed.
As we can see then, both the constant fields are displayed correctly and the Console.WriteLine() method has successfully passed the constant fields.
Example 04: Adding Two Constant Fields to Give Value to a Normal Variable in Ubuntu 20.04
In this example, we will be adding two constant fields, and the sum of these fields will be assigned to a normal integer, and that integer will be passed on in a method.
In the C# program, we will create two constant integer field values and then assign them some numbers; after this, we will initialize an integer variable whose value will be the sum of these two constant fields.
As we can see in the output, the sum of the integer was successfully interpreted by the compiler, and both the constant fields were added.
Example 05: Assigning a Constant Field a Value by Performing Mathematical Operations on Two Constant Fields in Ubuntu 20.04
We will now assign a constant field entity by dividing two different constant field entities. In this example, we will initialize two constant fields with values assigned and initialize another constant field and assign it with a mathematical expression of the two previous constant fields and see how the value is interpreted. So, we will be using the number of days and weeks in a year which is constant, to calculate the number of days in a week which is also a constant entity.
In the above C sharp program, we will be initializing two constant fields, “Weeks” and “Days” and assign them values “52” and ”365” respectively. We will initialize another constant field named “Days in a Week” and assign it a value; we will use the previous two constant fields and divide them with each other to get a value.
As we can see in the output, the value of the “Days in a Week” constant field is correctly calculated and was successfully passed through the method.
This article discussed the ”const” keyword in the C# programming language. This keyword is used to classify a constant field in a C# program. The “const” keyword is a part of the constant class in the C# programming language. We also implemented several examples of several types of variables for classifying them as a constant field and performed different operations on these fields to analyze the working of a constant field.