A brief introduction to constants type inference and scopes in C

  • 2020-05-27 07:02:26
  • OfStack

1. The constant
A constant is a variable whose value does not change during use. When a variable is declared and initialized, it can be specified as a constant by the keyword const before the variable:

const int a = 100; //a value cannot be changed

Features of constants:

1. Constants must be initialized when declared. Once the value is specified, it cannot be modified.
The value of the constant must be computable at compile time. Therefore, you cannot initialize a constant by extracting a value 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 need to include the modifier static in the declaration of constants. (actually, not allowed)
There are at least three benefits to using constants in a program:

1. Constants replace ambiguous Numbers or strings with clear names that are easy to understand, making the program easier to read.
2. Constants make programs easier to modify. For example, in the C# program there is an SalesTax constant with a value of 6%. If the sales tax rate changes later, assign the new value to this constant and you can modify all tax calculations without having to go through 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 your program that already has a value, the compiler reports an error.
As follows:

namespace Test
    class ScopeTest
        static int j=20;
        const string time= DateTime.Now.ToString();
        public static void Main()
            int j=30;

Error after compilation:

error CS0133: the expression assigned to "Test.ScopeTest.time" must be constant.

For time in the above code, you can assign it the readonly property if you want.

Both constants and read-only are actually accessible and cannot be modified. However, their assignment timing is not quite the same as that of 1, whose constants are determined and assigned constant values at compile time. Read-only is actually a variable that is assigned a value when it needs to be dynamically loaded at run time, and the value of 1 denier cannot be changed.

2. Type inference
Type inference USES the var keyword. The syntax for declaring variables has changed. The compiler can "infer" the type of a variable based on its initialization value, for example:

int someNumber=0;

It becomes

var someNumber=0;

Even if someNumber is never declared as int, the compiler can be sure that it is one int as long as someNumber is in its scope. After compilation, the above two statements are equivalent.

Here's another example:

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());

Compile the runtime :(see C# introduction for how to compile the program)

name is type System.String

age is type System.Int32

isRabbit is type System.Boolean

Defining variables using var requires a few rules. The variable must be initialized. Otherwise, the compiler has no basis to infer the variable type. The initializer cannot be empty and must be placed in the beginning of the expression. You cannot set the initializer to 1 object unless you create a new object in the initializer.

3. Scope of variables
The scope of a variable is the code area where the variable can be accessed. 1 in general, there are 1 rules for scoping:

1. As long as a class is in a scope, its fields are also in that scope
2. A local variable exists in scope before the closed curly braces that end the block statement or method in which the variable is declared.
3. Local variables declared in for, while, or similar statements exist within the loop
It is common for different parts of a large program to provide the same variable name for different variables. As long as variables are scoped to different parts of the program, there is no problem. It doesn't blur. Note, however, that local variables with the same name cannot be declared twice in the same 1 scope, so the following code cannot be used:

int x=20;

int x=30;

Here's another example:

using System;
namespace jb51
   pulic static int main()
       For(int i=0;i<10;i++)
       For(int i=0;i>=10;i - 

This code needs our attention. i appears twice, but they are both variables relative to the body of the loop.

Another example:

public static int Main()
  int j=20;
  for(int i=0;i<10;i++)
     int j=30;// error 
  return 0;

j there mistakes will happen, because the variables are defined before for cycle began, when performing for cycle should be in its scope, after Main method was implemented, variable j just beyond the scope, the second j (illegal) in circulation within the scope of nested scopes in within the scope of the Main approach, the compiler cannot distinguish the two variables.

Scope conflict for a field or local variable: in some cases, you can distinguish between two identifiers of a scoped item with the same name (although its fully qualified name is different). The compiler allows you to declare a second variable at this point. The reason is that C# makes a basic distinction between variables by treating variable fields declared at the type level as fields and variables declared in methods as local variables.

Related articles: