在C#中怎么样声明全局变量?

对,没有全局变量这个概念了 
一般的做法是把全局变量全部封装到一个类中,使用static就可以了。 
各位高手有什么意见

 

public   class   GVar 
{ 
        public   static   string   szName   =   " "; 
        public   static   int   nAge   =   0; 
} 

//   在这个类里面使用GVar类中的变量 
public   class   MyForm   :   System.Windows.Forms 
{ 
    ... 

    public   void   fun() 
    {     //   这里相当于使用全局变量(其他地方用法也一样) 
            GVar.szName   =   "Bill "; 
            GVar.nAge   =   10; 
    } 
}

  

 

原文地址:https://www.cnblogs.com/carl2380/p/2681127.html