静态类的静态方法与非静态类的静态方法 (C#) [英] Static Method of a Static Class vs. Static Method of a Non-Static Class ( C# )
问题描述
我在一次采访中被问到了上述问题.你能解释一下这些差异吗?(性能 - 内存 - 使用 - 什么时候使用哪个?)
I was asked the above question in an interview. Could you please explain the differences? ( performance - memory - usage - when to use which ? )
谢谢,
尔坎
推荐答案
声明一个静态类记录了您希望该类成为静态功能集合的意图,任何添加实例成员的人都会收到编译错误.
Declaring a static class documents your intent for that class to be a collection of static functionality, and anyone adding instance members will get a compilation error.
具有静态成员的非静态类通常表示该类旨在在某个时刻实例化.这些类的静态方法通常做两件事之一:
A non-static class with static members usually indicates that the class is designed to be instantiated at some point. Static methods of these classes usually do one of two things:
- 提供用于创建该类型实例的工厂方法;
- 提供不需要类型实例的辅助功能;
另外,如前所述,扩展方法只能在静态类上声明.
Also, as mentioned already, extension methods can only be declared on a static class.
这篇关于静态类的静态方法与非静态类的静态方法 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!