静态类的优点 [英] Advantage of Static Class

查看:88
本文介绍了静态类的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道什么是静态类但是拥有静态类的优点

它与应用程序的速度(执行时间)有任何关系.... ..

I know What Static class is but what is the advantage of having static class
Does it has any relation to speed of application(execution time)......

推荐答案

当加载包含类的程序或命名空间时,.NET Framework公共语言运行库(CLR)会自动加载静态类。

类可以声明为static,表示它只包含静态成员。使用new关键字无法创建静态类的实例。

使用静态类来包含与特定对象无关的方法。例如,创建一组不对实例数据起作用且与代码中的特定对象无关的方法是一种常见的要求。您可以使用静态类来保存这些方法。

静态类的主要功能是:

•它们只包含静态成员。

•它们无法实例化。

•它们是密封的。

•它们不能包含实例构造函数。

因此创建静态类很多与创建仅包含静态成员和私有构造函数的类相同。私有构造函数阻止实例化类。

使用静态类的优点是编译器可以检查以确保不会意外添加实例成员。编译器将保证无法创建此类的实例。

静态类是密封的,因此不能继承。静态类不能包含构造函数,尽管仍然可以声明静态构造函数来分配初始值或设置一些静态状态。



更多请参阅此静态类和静态类成员
Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword.
Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.
The main features of a static class are:
• They only contain static members.
• They cannot be instantiated.
• They are sealed.
• They cannot contain Instance Constructors .
Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.
The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.
Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state.

For more See this Static Classes and Static Class Members


这篇关于静态类的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆