c#的静态变量是在对象生成的时候分配内存空间的,而不是函数执行的时候。

如果在函数里定义,那么这个变量就需要在函数执行的时候分配内存空间,这是C#不允许的,至于为什么不允许,个是因为垃圾回收机制的问题。

class   Studetn

{

     static string Name;

     public string GetName()

     {

          

     } 

   以上是正确的

    public static string  UserName()

    {

                static string _name;//错误的

    }

}

 

相关文章:

  • 2022-12-23
  • 2021-12-26
  • 2021-10-12
  • 2022-12-23
  • 2022-01-24
  • 2021-09-19
  • 2021-07-16
  • 2022-12-23
猜你喜欢
  • 2021-06-06
  • 2021-06-09
  • 2021-08-25
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
相关资源
相似解决方案