CLR如何处理静态类? [英] How does the CLR handles static classes?

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

问题描述

谁能解释CLR如何处理静态类? CLR是否创建一个用于内部处理静态类的单例实例?如果没有,为什么我们在C#中有一个静态构造函数? (据我了解,我们仅将构造函数用于实例化该类)。

Can anyone explain how the CLR handles Static classes? Does the CLR create one singleton instance for handling static classes internally? If not, why do we have a static constructor in C#? (Per my understanding, we use constructors only for instantiating the class)

推荐答案

首先,CLR中没有静态类。 CLR对静态类一无所知。
这是C#的功能。

First of all there is no static class in CLR. CLR doesn't know anything about static class. It is the feature of C#.

静态类被编译为抽象以及密封类。将其设为抽象可防止实例化,而使其 sealed 可防止继承。

Static classes are compiled into abstract as well as sealed class. Making it abstract prevent instantiation of it and sealed prevents inheritance.

静态类没有特殊之处,它只是一个简单的类,所有成员都是静态的。

Static classes are no special, it is just a simple class with all members static.


是的,CLR在内部创建一个单例实例用于处理静态
类?

Is, CLR internally creates one singleton instance for handling static classes?

,它是抽象的,无法实例化

No, it is abstract and can't be instantiated.


如果不是为什么我们在C#中有一个静态构造函数?

If not why we have an static constructor in C#?

在第一次引用或实例化type时将调用静态构造函数。

Static constructor will be called when type is first referenced or instantiated. It is used to initialize the static members of the class.

When is a static constructor called in C#?

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

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