静态类的静态方法与非静态类的静态方法 (C#)

发布于 2024-08-21 19:19:03 字数 90 浏览 1 评论 0原文

我在一次采访中被问到了上述问题。您能解释一下这些差异吗? (性能 - 内存 - 使用情况 - 何时使用哪个?)

谢谢你,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

温柔戏命师 2024-08-28 19:19:03

声明静态类记录了您将该类作为静态功能集合的意图,任何添加实例成员的人都会收到编译错误。

具有静态成员的非静态类通常表明该类被设计为在某个时刻被实例化。这些类的静态方法通常执行以下两件事之一:

  1. 提供用于创建该类型的实例的工厂方法;
  2. 提供不需要类型实例的辅助功能;

此外,正如已经提到的,扩展方法只能在静态类上声明。

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:

  1. Provide a factory method for creating an instance of that type;
  2. Provide helper functionality that does not require an instance of the type;

Also, as mentioned already, extension methods can only be declared on a static class.

氛圍 2024-08-28 19:19:03

我想你会被问到差异吗?

静态类上的静态方法可用于定义扩展方法。非静态类上的静态方法不能。

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.

鸠书 2024-08-28 19:19:03

在性能和内存使用方面;完全没有。拥有静态类意味着您知道没有实例,但回到 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)

初懵 2024-08-28 19:19:03

当您提供实用程序函数并且所有方法都是静态时,我建议您在静态类中使用静态方法。

当您想提供仅处理实例的实用方法时,我建议您在非静态类中使用静态方法。例如:

var myClass = MyClass.Create();
var myClass = MyClass.Parse("serialized.MyClass");

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:

var myClass = MyClass.Create();
var myClass = MyClass.Parse("serialized.MyClass");
迷途知返 2024-08-28 19:19:03

在决定是使用带有所有静态方法的普通类还是使用静态类时,我面临的一个主要区别是普通类支持接口实现,而静态类则不支持。
仅当我确定静态类将是静态函数(通常是辅助函数)的集合,并且永远不会出现在程序的主流中时,我才使用静态类。我提倡接口编程,用于依赖注入、单元测试等。因此,对于程序的主要流程,我使用带有静态方法的普通类。

参考: 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

撕心裂肺的伤痛 2024-08-28 19:19:03

在内存方面,存在细微差别:非静态类中的静态方法仅在创建该类型的第一个实例时才会分配,并在该类型的最后一个实例被释放时释放。当我们拥有相同类型的对象集合以减少内存使用量时,实例对象上的静态方法非常有用。使用静态方法的缺点是它们不可进行单元测试,因此在创建静态方法之前,应密切关注它将如何影响代码覆盖率。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文