On constants, type inference and scopes in C #
Font: [Increase decrease] Type: Reprint time: 2013-12-19 I want to comment
This article focuses on constants, type inference, and scopes in C #, and the friends you need can refer to
One, constant
A constant is a variable whose value is not changed during use. When declaring and initializing a variable, the variable is preceded by the keyword const, which can be specified as a constant:
The value of the const int a=100;//a will not be changed
Characteristics of constants:
1. Constants must be initialized at the time of declaration. After you specify its value, you can no longer modify it.
2. The value of a constant must be used for calculation at compile time. Therefore, constants cannot be initialized from values that are extracted from a variable. If you need to do this, you should use a read-only field.
3. Constants are always static, but note that you do not have to include modifier static in the declaration of a constant. (In fact, not allowed)
There are at least 3 benefits to using constants in your program:
1. Constants replace ambiguous numbers or strings with clear, easy-to-understand names, making the program easier to read.
2. Constants make the program easier to modify. For example, there is a Salestax constant in a C # program that has a value of 6%. If the sales tax rate changes in the future, assigning the new value to the constant, you can modify all of the tax calculation results without having to find the entire program and modify each item with a tax rate of 0.06.
3. Constants make it easier to avoid program errors. If you assign another value to a constant in the program, and the constant already has a value, the compiler reports an error.
such as the following procedures:
Copy CodeThe code is as follows:
Namespace Test
{
Class Scopetest
{
static int j=20;
Const string Time= DateTime.Now.ToString ();
public static void Main ()
{
int j=30;
Console.WriteLine (j);
Return
}
}
}
Error generated after compilation:
Error CS0133: The expression assigned to "Test.ScopeTest.time" must be a constant.
For the time in the code above, you can assign it to the ReadOnly property, if necessary.
Constants and read-only are only accessible and cannot be modified. But their assignment timing is different, and the general constants are determined and given constant values at compile time. And read-only is actually a variable. He assigns a value to a dynamic load when it is run, and the value can no longer be changed once it is assigned to it.
Ii. type Inference
Type inference uses the var keyword. There are some changes to the syntax of declaring variables. The compiler can "infer" the type of a variable based on the initialization value of the variable, for example:
int somenumber=0;
will become
var somenumber=0;
Even if Somenumber is never declared int, the compiler can also determine that, as long as the somenumber is within its scope, it is an int. After compiling, the above two statements are equivalent.
Here is another example:
Copy CodeThe code is as follows:
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
var name = "Bugs Bunny";
var age=25;
var israbbit=true;
Type Nametype=name. GetType ();
Type Agetype=age. GetType ();
Type Israbbittype=israbbit.gettype ();
Console.WriteLine ("Name is Type" +nametype.tostring ());
Console.WriteLine ("Age is Type" +agetype.tostring ());
Console.WriteLine ("Israbbit is Type" +israbbittype.tostring ());
Console.readkey ();
}
}
}
Compile and run the program: (How to compile the program please refer to C # introductory article)
Name is type System.String
Age is Type System.Int32
Israbbit is type System.Boolean
It takes some rules to define variables using var. Variables must be initialized. Otherwise, the compiler has no basis for inferring the variable type. The initializer cannot be empty and must be placed in the beginning of the expression. The initializer cannot be set to an object unless a new object is created in the initializer.
Iii. Scope of variables
The scope of a variable is the area of code that can access the variable. In general, determine the scope of the rule:
1. As long as the class is within a scope, its fields are also within that scope
2. A local variable exists in the scope before the enclosing curly brace that declares the variable's block statement or method ends.
3. A local variable declared in a for, while, or similar statement exists with the inside of the loop
It is common for different parts of a large program to provide the same variable names for different variables. As long as the scope of the variable is different parts of the program, there is no problem. Nor does it produce ambiguity. Note, however, that a local variable of the same name cannot be declared two times within the same scope, so the following code is not available:
int x=20;
int x=30;
Let's look at the following example:
Copy CodeThe code is as follows:
Using System;
Namespace jb51
{
pulic static int main ()
{
for (int i=0;i<10;i++)
{
Console.writelie (i);
}
for (int i=0;i>=10;i-
{
Console.WriteLine (i);
}
}
}
This code needs to be brought to our attention. I appeared two times, but they were all variables relative to the loop body.
Another example:
Copy CodeThe code is as follows:
public static int Main ()
{
int j=20;
for (int i=0;i<10;i++)
{
int j=30;//Error
Console.WriteLine (J+i);
}
return 0;
}
An error occurs here because the variable j is defined before the for loop, should be in its scope when the For loop is executed, the variable J goes out of scope after the main method executes, and the second J (illegal) is within the scope of the loop, nested within the scope of the main method in scope, The compiler cannot differentiate between these two variables.
Scope conflict for a field or local variable: In some cases, you can distinguish between the same name (although its fully qualified name differs), and the two identities of the scope item. The compiler now allows the declaration of the second variable. The reason is that C # has a basic distinction between variables, which treat variable fields declared as type-level as fields, and variables declared in a method as local variables.
On constants, type inference and scopes in C #