1. Constant
A constant is a variable whose value does not change during use. When declaring and initializing a variable, the keyword const is in front of the variable, and the variable can be specified as a constant:
const int a=100;//The value of a will not be changed
Characteristics of constants:
1. Constants must be initialized at declaration time. After specifying its value, it cannot be modified.
2. The value of a constant must be used for calculation at compile time. Therefore, the constant cannot be initialized from the value extracted from a variable. If you need to do this, you should use read-only fields.
3. Constants are always static, but note that you do not have to include modifier static in the declaration of the constant. (In fact, it is not allowed)
There are at least 3 benefits to using constants in a program:
1. Constants replace unclear numbers or strings with easy-to-understand clear names, making the program easier to read.
2. Constants make the program easier to modify. For example, in a C# program, there is a SalesTax constant, and the value of this constant is 6%. If the sales tax rate changes in the future and assign the new value to this constant, you can modify all tax calculation results without having to look up the entire program and modify each item with a tax rate of 0.06.
3. Constants are easier to avoid program errors. If you want to assign another value to a constant in the program, and the constant already has a value, the compiler will report an error.
As in the following procedure:
namespace Test
{
class ScopeTest
{
static int j=20;
const string time= ();
public static void Main()
{
int j=30;
(j);
return;
}
}
}
An error occurred after compilation:
error CS0133: The expression assigned to "" must be a constant.
For the time in the above code, if necessary, you can assign it to the readonly attribute.
Constants and read-only are actually accessible and cannot be modified. However, their assignment timing is different. Generally, constants have been determined and assigned to their constant values at compile time. Read-only is actually a variable that only gives it a value when it needs to be loaded dynamically at runtime, and once this value is assigned, it cannot be changed.
2. Type inference
Type inference uses the var keyword. There are some changes in the syntax for declaring variables. The compiler can "infer" the type of a variable based on the initialization value of the variable, for example:
int someNumber=0;
It becomes
var someNumber=0;
Even if someNumber is never declared as an int, the compiler can determine that as long as someNumber is within its scope, it is an int. After compilation, the above two statements are equivalent.
Here is another example:
namespace Test
{
class Program
{
static void Main(string[] args)
{
var name ="Bugs Bunny";
var age=25;
var isRabbit=true;
Type nameType=();
Type ageType=();
Type isRabbitType=();
("name is type "+());
("age is type "+());
("isRabbit is type"+());
();
}
}
}
Compile and run the program: (Refer to the C# entry to how to compile the program)
name is type
age is type System.Int32
isRabbit is type
Defining variables using var requires some rules. The variable must be initialized. Otherwise, the compiler has no basis to infer variable types. The initializer cannot be empty and must be placed in the expression beginning. The initializer cannot be set as an object unless a new object is created in the initializer.
3. The scope of variables
The scope of a variable is the code area where the variable can be accessed. Generally speaking, there are the following rules to determine the scope:
1. As long as the class is within a scope, its fields are also within that scope
2. Local variables exist within the scope before the closed curly braces at the end of the block statement or method that declares the variable.
3. Local variables declared in for, while or similar statements exist in the loop body
It is common for different parts of a large program to provide the same variable name for different variables. As long as the scope of the variable is a different part of the program, there will be no problem. There will be no blur. But be aware that local variables with the same name cannot be declared twice within the same scope, so the following code cannot be used:
int x=20;
int x=30;
Let’s take a look at the following example:
using System;
namespace jb51
{
pulic static int main()
{
For(int i=0;i<10;i++)
{
(i);
}
For(int i=0;i>=10;i—
{
(i);
}
}
}
This code needs to be taken care of. i appears twice, but they are both variables relative to the loop body.
Another example:
public static int Main()
{
int j=20;
for(int i=0;i<10;i++)
{
int j=30;//Error
(j+i);
}
return 0;
}
An error will occur here because the variable j is defined before the for loop starts. It should be within its scope when executing the for loop. After the Main method is executed, the variable j will be out of scope. The second j (unlawful) is within the scope of the loop. When the scope is nested in the scope of the Main method, the compiler cannot distinguish these two variables.
Scope conflicts of fields or local variables: In some cases, two identities of the scope item can be distinguished from the same name (although their fully qualified names are different). The compiler allows the declaration of the second variable at this time. The reason is that C# has a basic distinction between variables, which regards variable fields declared as type-level as fields, and variables declared in methods as local variables.