静态类的静态方法与非静态类的静态方法 (C#)
我在一次采访中被问到了上述问题。您能解释一下这些差异吗? (性能 - 内存 - 使用情况 - 何时使用哪个?)
谢谢你,
Erkan
I was asked the above question in an interview. Could you please explain the differences? ( performance - memory - usage - when to use which ? )
Thank you,
Erkan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
声明静态类记录了您将该类作为静态功能集合的意图,任何添加实例成员的人都会收到编译错误。
具有静态成员的非静态类通常表明该类被设计为在某个时刻被实例化。这些类的静态方法通常执行以下两件事之一:
此外,正如已经提到的,扩展方法只能在静态类上声明。
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.
我想你会被问到差异吗?
静态类上的静态方法可用于定义扩展方法。非静态类上的静态方法不能。
I assume you were asked for the differences?
A static method on a static class can be used to define an extension method. A static method on a non-static class cannot.
在性能和内存使用方面;完全没有。拥有静态类意味着您知道没有实例,但回到 1.1 时,拥有私有构造函数就足够了。如果拥有实例根本没有意义,请使用静态类! (实用类等)
In terms of performance and memory usage; precisely nothing. Having a static class means you know there are no instances, but back in 1.1 having a private constructor sufficed. Use a static class if it simply makes no sense to have an instance! (utility classes etc)
当您提供实用程序函数并且所有方法都是静态时,我建议您在静态类中使用静态方法。
当您想提供仅处理实例的实用方法时,我建议您在非静态类中使用静态方法。例如:
When you are providing utility functions and all your methods are static, I recommend you use static methods in a static class.
When you want to provide utility methods that just deal with your instance, I recommend you use static methods in a non-static class. For example:
在决定是使用带有所有静态方法的普通类还是使用静态类时,我面临的一个主要区别是普通类支持接口实现,而静态类则不支持。
仅当我确定静态类将是静态函数(通常是辅助函数)的集合,并且永远不会出现在程序的主流中时,我才使用静态类。我提倡接口编程,用于依赖注入、单元测试等。因此,对于程序的主要流程,我使用带有静态方法的普通类。
参考: MS 文档
One major difference I faced when deciding whether to go with normal class with all static methods or, use a static class, is that a normal class supports interface implementation, where as static class does not.
I use static class only when I am sure it will be a collection of static functions (usually helper functions), and will never be in the main stream of program. I promote interface programming, for dependency injections, unit testing etc. So, for main flow of program, I use normal class with static methods.
Ref: MS Docs
在内存方面,存在细微差别:非静态类中的静态方法仅在创建该类型的第一个实例时才会分配,并在该类型的最后一个实例被释放时释放。当我们拥有相同类型的对象集合以减少内存使用量时,实例对象上的静态方法非常有用。使用静态方法的缺点是它们不可进行单元测试,因此在创建静态方法之前,应密切关注它将如何影响代码覆盖率。
In terms of memory, there is a slight difference: the static method in a non-static class will be allocated only when the first instance of that type is created, and deallocated when the last instance of that type is deallocated. Static methods on instance objects are very useful when we have collections of objects of the same type in order to decrease the amount of memory used. The drawback of using static methods is that they are not unit testable, so before creating a static method, an eye should be kept on how it will affect the code coverage.