为什么使用静态类?
我对静态类和静态方法有疑问。从 MSDN 中我了解到“静态类和类成员用于创建无需创建类实例即可访问的数据和函数”。
因此,如果我们不想将类与实例关联起来,我们会将其设置为静态。这是唯一的优点吗?谁能指导我在哪个实时场景中进行静态类。
有时在课堂上(非静态)我发现静态方法。在实际中,静态方法相对于实例方法有哪些优势/性能优势?
I have doubts on static class and static methods. From MSDN I understood that "Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class."
So if we don't want to associate a class over an instance , we will make it as static. Is that the only advantage? Can anyone guide me in which real time scenario we go for static class.
Some time in classes(not static) I am finding static methods. What advantage/perfomance advantage do static methods give over instance methods in practical.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于实用类来说,它们非常棒。正如您提到的,它们类似于全局状态。因此,对于没有状态的类,为了提高性能,该类应该是静态的。
另一方面,静态类很难测试(如果它们包含状态)。多态性和其他面向对象的概念也丢失了。
明智地使用。
For utility classes they are great. As you mentioned, they are similiar to global state. So for classes which have no state, for performance benefits the class should be static.
On the other hand, static classes are hard to test (if they contain state). Polymorphism and other OO concepts are also lost.
Use wisely.
将 static 关键字应用于类是 C# 语言约定,它对 CLR 没有任何特殊意义。它只是确保所有成员也是静态的,并且您不会意外地使用 new 关键字创建类的实例。
此线程讨论了静态方法的优点。
Applying the static keyword to a class is a C# language convention, it doesn't mean anything special to the CLR. It merely makes sure that all members are static as well and that you can't accidentally create an instance of the class with the new keyword.
The merits of static methods are discussed in this thread.
IMO 静态类是变相的过程编程。不一定是坏事,但它不是很 OOPly。注意函数式分解反模式。
IMO static classes are procedural programming in disguise. Not necessarily a bad thing, but it's not very OOPly. Watch out for the functional decomposition antipattern.
静态类非常适合定义静态方法。这是经典的“实用类”方法。但是,在静态类中存储状态(即定义字段)时要格外小心。在我们的多线程世界中,这可能会导致不可预测的程序行为,除非您同步对静态字段的访问。
Static classes are great for defining static methods. This is classic 'utility class' approach. However, be extremely careful with storing state (i.e. defining fields) in a static class. In our multi-threaded world this can lead to unpredictable program behavior unless you synchronize access to static fields.
静态属性主要用于引入运行代码的Context。
您可以在 .NET 堆栈的每个部分中找到这一点的确认。
ASP.NET - HttpContext.Current
线程 - Thread.CurrentThread
WinForms - WindowsFormsSynchronizationContext.Current
WPF - 调度程序
等等
对我来说,静态类只是实用方法的容器。
Static properties mainly used to introduce Context of running code.
And you can find confirmation for that in every piece of .NET stack.
ASP.NET - HttpContext.Current
Threading - Thread.CurrentThread
WinForms - WindowsFormsSynchronizationContext.Current
WPF - Dispatcher
etc
static class for me is just container for utility methods.
静态类全局化特定变量,使其更容易在代码中处理。
因此,在基础层面上,我们更喜欢使用静态类。
Static Classes globalizes a particular variable which makes it easier to handle during the code.
Hence at basic level we prefer using Static Classes.