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

namespace learn_val_location
{
    internal class Program
    {
        // static的变量是全局变量,都可以访问到,也可以修改
        static int aaa = 123;

        // const表示一个常量,都可以访问到,不可以修改
        const int bbb = 123;

        // 
        int eee = 123;
        static void Main(string[] args)
        {
            // 函数内部的变量只有内部可以访问到
            int ccc = 123;

            // 变量的作用域
            int f;
            for (int i = 0; i < 10; i++)
            {
                f = 3;
            }
            // for循环中定义的变量在外边不能使用; 就算是在外边定义但是没初始化的也不能用
            Console.WriteLine(f);

            // if循环内部的可以访问到
            int h;
            if (true)
            {
                h = 3;
            }
            Console.WriteLine(h);

            void AAA()
            {
                Console.WriteLine(ccc); // 内部定义的函数
            }

            Console.WriteLine(eee); // 没有static修饰的变量在内部访问不到
        }
        AAA(); // 内部定义的函数外部访问不到
    }
    
}

posted on 2023-01-15 22:33  盈盈的月儿  阅读(48)  评论(0编辑  收藏  举报