Whenever we develop a program using any programming language, our basic aim is to solve some kind of Real World Problem.
But before solving any problem, we have to store the necessary data related to that problem in the computer’s memory because without storing the problem-related data in the computer’s memory, we do not do any kind of processing on it by our program. And unless Appropriate Processing is done on the data, no output is generated.
But before we store the problem-related data by inputting it in the computer’s memory, we have to preserve the memory of the appropriate size on the basis of our problem-related data in the computer’s memory and give a suitable name to that reserved memory location.
Because when we reserve a memory of Appropriate Size to store our Problem Related Data in Computer’s memory and specify the name of that Reserved Memory, our Compiler by the same name, that Reserved Memory during our entire Application Program identifies.
Not only this, whatever program we create, that program also reserves space somewhere in the memory of the computer and is executed only after being loaded in that space. Therefore, whatever is the name of our program file, by the name of that program file, the CompilerCompiler identifies the memory location in which the loaded program is being executed.
Therefore, our Compiler is able to identify the problem-related data and the memory location of the currently executed program file by their name. Therefore, we specify the name of the memory location and program file to store our data; those names are known as identifiers in Computer Programming Language.
That is, identifiers always represent the name of some memory location, which is related to the current program.
Like any other programming language, C# also has some rules related to defining the name of identifiers, which are as follows:
- The _ (Underscore) sign can be used in the name of any Identifier (Variable, Constant, Class, Delegate, Enum, Structure, Array, Method, etc.).
- The characters used in the name can be any type of uppercase or lowercase, and we can also mix these valid characters among themselves.
- Any special symbol like Dot, Comma, #, $, etc., can never be used in the name of identifiers.
- No name can ever start with a number, although numbers can be used in the middle or at the end of the name.
- If we want to use a keyword as an identifier, then we can do so, but in this case, it is necessary to start the keyword with @ symbol.
For example, the following names as identifiers are Valid Identifier Names:
- day of Week
- months of year
Similarly, the following names are incorrect, as these names do not fully comply with C#’s Naming Rules:
- days Of Week
- months OfYear10
Constants in C#
In any computer program, we store different types of values in the memory of the computer, manage them, apply the required processing on them and return their result as output. If we look in the real world, there are basically two types of values. One value is the one that can never be changed, while the other type of value can be changed.
As there is a total of 12 months in a year. The number of months is fixed. There can never be 11 or 13 months in any year. Similarly, every month has a definite name. There are seven days every week. Every day has a certain name. Similarly, the value of PI is 22/7. etc.-
We can understand that there are thousands of such things whose values are always fixed. Values that are always fixed, those values are called Constants or Literals. Similarly, the value used in a computer program that cannot change throughout the program is called Constant.
As a result, the memory location on which we do not want to change the stored value during the whole program, we declare that value as Constant Identifier and the identifier we declare as Constant, our CompilerCompiler takes care that During the entire program, the value stored on the memory location of that identifier should not change in any way.
Whereas if in any way an attempt is made to change the value stored at the memory location of this Constant Identifier during the execution of the entire program, the CompilerCompiler generates an error to prevent it from doing so.
We can consider any data as a value or a set of values. That is, to represent any fact in a computer program, we have to define that fact in the form of some value. Values in a computer are represented either as texts or as numbers.
For example, if we want to store the total days of the year on the computer, then we use the number 12, which is a number. Whereas if we want to store the name of a month as “January” in the computer, then we use a group of characters.
On the basis of this example, we can say that any real-world value is defined in the computer either as a number or as a string, whereas in the language of computer programming, one or more groups of characters. It is called a String.
We can call the group of different Digits Numeral Constants, and the group of different Characters can be called String Constants. For example, suppose we want to find 6.0 percent interest on Rs.100. We can perform this calculation as follows:
Interest = 100 * 6.0 / 100
Numerical values 100 and 6.0 are constant values in this line, so they are called Constant.
Suppose we have to perform this calculation many times in a program, then in this situation, we can write this statement many times in the whole program. But after a while, if we get instead of 6.0; This program has to be modified to calculate 7.0 percent interest, so the number of times we have performed this calculation in the program, we will have to replace 7.0 instead of 6.0.
That is, if we have used this statement 200 times in our program, then we will have to do 7.0 instead of 6.0 at 200 places in our program. It takes a lot of time to modify a program in this way, and there is a lot of possibility of mistakes.
Therefore, to avoid such situations, programmers always use some Symbolic Constants, which are usually words that represent a value in the program.
In relation to this statement of ours, to represent 6.0, we can use a Symbolic Content named Percent, which represents Current Percent, and at the beginning of the program, in Percent, we specify the rate, which is used for the whole program. To calculate in The statement which is written to represent a Constant in C# is called Declaring Constant, and it is declared as follows:
const float PERCENT = 6.0;
The const keyword in C# is used when we have to tell the C# compiler that we want to keep a value constant or constant throughout the program. Also, the word const also tells the C# compiler that the value of the constant we want to store is a floating-point value or a decimal value.
The word PERCENT is symbolic content. In this statement, the sign of = indicates that the word which is on the left side of the =, that word is equal to the value which is on the right side of the = sign, which is 6.0 in our statement.
That is, we write 6.0 or PERCENT in any calculation; the result coming out of both will be the same because both are representing the same value. Therefore, if we want, we can also write the previous statement to find out the Interest as follows:
Interest = 100 * PERCENT / 100;
Generally, Symbolic Constants are written in UPPERCASE letters to make them stand out from other codes of the program.
The advantage of specifying Symbolic Constant in this way is that no matter how many times we have used this constant throughout our program, we only have to replace 6.0 with 7.0 by the following statement:
const float PERCENT = 7.0;
And our current program itself starts calculating Interest at the rate of 7.0 percent instead of 6.0.
Variables in C#
The values of the program, which keep changing from time to time as per the requirement during the execution of the whole program, are called Variable Identifiers. Variables are never used to represent a constant value.
Whenever we have to use a Constant in the program, we need Symbolic Constants to represent that Constant. These Symbolic Constants are also called Literal. The question arises that to understand what the variables are needed in the program, let’s take the previous statement, which is as follows:
Interest = 100 * PERCENT / 100;
Interest is a variable in this statement. That is, to store the result of a calculation, we always need such a memory location in our program, in which such values can be stored, which can be changed by different types of statements during the execution of the program.
By this statement, we can find the PERCENT of 100 only. But generally, we have to find the Percent of different types of values at different places. In such a situation, if we have to make a separate program to find the percentage of each number, then it will be a very inconvenient thing.
The program should be such that different types of calculations can be performed in the same way from the same program. That is, if we make 200 instead of 100, then we will get an interest of 200. If we want to make the program multipurpose, then we also have to represent 100 in some symbolic way. We can do this work by the following statement:
Principal = 100; Interest = Principal * PERCENT / 100;
We can see that if the value of Principal is changed from 100 to 200, then we will get the Interest of Principal 200 in the variable named Interest.
Since the value of the Symbolic Representative Principal of the principal 100 can be changed to 200, 300, 400, etc., hence the Principal is a variable, and the change of Principal also changes the calculated Interest. Hence Interest is also a variable.