C# | C# variable (local variable) declaration and scope

Global variables are often declared in the Main function, and their scope is the entire code; local variables exist in the scope before the closing curly brace ({}) at the end of the block statement or method in which the variable is declared. This article borrows several demos to show some variable declarations and scope relationships in C#.
Case 1: Variable contains ambiguous

According to the principle of variable scope (local variables exist in the scope before the closing curly brace ({}) of the block statement or method that declares the variable), the first variable j is defined before the start of the for loop. Its scope is the Main() method; the scope of the second variable is in the for loop; the scope of the first variable j includes the scope of the second j. There will be ambiguity for the compiler, because the compiler cannot distinguish between these two variables, so it is not allowed to declare the second variable.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200226bianliang
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int j = 20;//在Main中定义j为全局变量
            for(int i=0; i<10; i++)
            {
    
    
                int j = 30;//在for循环中定义j为局部变量,但两者同为Main中,产生了包含与被包含的关系,顾报错。
                Console.WriteLine(j);
            }
        }
    }
}
Case 2: Variables are upgraded to "class-level variables"

C# has a basic distinction between variables. Variables declared at the class level are regarded as fields (member variables), and variables declared in methods are regarded as local variables.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200226bianliang
{
    
    
    class Program
    {
    
    
        public int j = 20;//在Main外声明了一个类级变量
        static void Main(string[] args)
        {
    
    
            int j = 30;//在Main函数中定义了一个Main中的变量
            Console.WriteLine(j);//执行主函数Main,打印Main中的j
            return;
        }
    }
}
Situation 3: Reference class-level variables in the main function

For case two, before the first j is defined to the Main function through public, it is "upgraded" to a class-level variable (this time belongs to a local variable), and the application of class-level variables can be realized by the following method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200226bianliang
{
    
    
    class Program
    {
    
    
        public int j = 20;
        static void Main(string[] args)
        {
    
    
            int j = 30;
            Console.WriteLine(j);
            Program obj = new Program();//new创建对象
            Console.WriteLine("类级变量是:" + obj.j);//调用方法,引用类级变量j
            return;
        }
    }
}
Situation 4: static declaration introduces class-level variables

If you choose to declare the field as static, you can directly introduce class-level variables when calling.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200226bianliang
{
    
    
    class Program
    {
    
    
        static int j = 20;//static声明
        static void Main(string[] args)
        {
    
    
            int j = 30;
            Console.WriteLine(j);
            //Program obj = new Program();
            Console.WriteLine("类级变量是:" + Program.j);//可直接调用
            return;
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_44421798/article/details/104524676