简体   繁体   English

静态类和静态方法

[英]Static Class & Static Method

静态类中的静态方法和非静态类中的静态方法有什么不同?

There is no difference. 没有区别。
Not between the methods anyway. 反正不是方法之间。

For the majority of scenarios they are essentially equivalent features. 对于大多数情况,它们本质上是等效的特征。 The one notable difference is that only a static method in a static class can be an extension method. 一个值得注意的区别是static类中只有static方法可以是扩展方法。

There is no difference. 没有区别。

Unlike a non-static class you cannot modify the static method to be an instance method later, because a static class can only contain static members. 与非静态类不同,您不能在以后将静态方法修改为实例方法,因为静态类只能包含静态成员。

This is not allowed and will not compile 这是不允许的,不会编译

public static class Foo{
    public void test(){}
}

A static class can contain only static methods, whereas a non-static class can contain both. 静态类只能包含静态方法,而非静态类可以包含两者。 Deciding which way to go is strictly a design issue in C#. 决定走哪条路是严格意义上的C#设计问题。

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

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