简体   繁体   English

静态类中的静态变量和非静态类中的静态变量

[英]static variable inside a static class and a static variable inside a non static class

what is the difference between a static variable inside a static class and a static variable inside a non static class? 静态类中的静态变量和非静态类中的静态变量之间有什么区别?

ex: 例如:

public static class GT
{
   public static readonly string x;
}


public class GT
{
   public static readonly string x;
}

There is no difference when you're talking about the actual variables inside the class. 当你在讨论类中的实际变量时没有区别。 A static variable inside a static class is the same as a static variable inside a non static class. 静态类中的静态变量与非静态类中的静态变量相同。

On the other hand.. if you're talking about the classes themselves, then the static class cannot contain instance variables or methods.. it doesn't make sense.. since you cannot instantiate an instance of a static class yourself. 另一方面..如果您正在讨论类本身,那么静态类不能包含实例变量或方法..它没有意义..因为您无法自己实例化静态类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM