Static classes becomes abstract sealed in IL code but we can not create abstract sealed class in our C# code

huangapple go评论62阅读模式
英文:

Static classes becomes abstract sealed in IL code but we can not create abstract sealed class in our C# code

问题

我在使用C#的静态类时遇到了一些困惑。

我在C#中创建了一个静态类,但当我查看IL代码时,它变成了一个抽象密封类。

//以下是C#代码
internal static class Converter
//以下是IL代码
.class private auto ansi abstract sealed beforefieldinit StaticExample.Converter
    extends [System.Runtime]System.Object

但是当我尝试在C#中创建一个同时使用abstract和sealed关键字的类时,它不允许这样做。

英文:

I've had a confusion while working with the static classes in C#.

I've created a static class in C# and when I checked the IL code it becomes abstract sealed class.

//C# Code below 
internal static class Converter

//IL Code Below
.class private auto ansi abstract sealed beforefieldinit StaticExample.Converter
	extends [System.Runtime]System.Object

But when I try to create a class in C# with abstract and sealed keywork it's not allowing to do so.
Static classes becomes abstract sealed in IL code but we can not create abstract sealed class in our C# code

答案1

得分: 1

从这个 Microsoft.com 上的 PDF 中,描述了CIL标记(在第10.1.4节中引用)。您应该了解的是,通用中间语言(CIL)与具有相同命名的C#代码没有一对一的映射 - 在这种情况下,单词 abstract 在这两种语言中的含义有一些不同。CIL标记:

指定特殊语义的属性是 abstractsealed。这些属性可以一起使用。

abstract 指定此类型不得实例化。如果一个类型包含抽象方法,那么该类型应声明为抽象类型。
sealed 指定类型不得有派生类。所有值类型都必须是 sealed。

[理由:密封类型的虚拟方法实际上是实例方法,因为它们不能被重写。框架作者应谨慎使用密封类,因为它们不提供方便的用户扩展构建块。密封类在一组虚拟方法的实现(通常是多个接口)变得相互依赖或对潜在派生类不可见的实现细节至关重要时可能是必需的。
既是 abstract 又是 sealed 的类型应该只包含静态成员,并且充当某些语言称为 "命名空间" 或 "静态类" 的东西。结束理由]

英文:

From this PDF on Microsoft.com, there are described CIL tokens (quote below; from section 10.1.4). What you should understand is, that the Common Intermediate Language (CIL) doesn't represent 1 to 1 mapping to C# code with same naming - in this case the word abstract has a little different meaning in each of those languages. CIL Tokens:

> Attributes that specify special semantics are abstract and sealed. These attributes can be used together.
>> abstract specifies that this type shall not be instantiated. If a type contains abstract methods, that type
shall be declared as an abstract type.
> > sealed specifies that a type shall not have derived classes. All value types shall be sealed.

> > [Rationale: Virtual methods of sealed types are effectively instance methods, since they cannot be overridden.
Framework authors should use sealed classes sparingly since they do not provide a convenient building block
for user extensibility. Sealed classes can be necessary when the implementation of a set of virtual methods for
a single class (typically multiple interfaces) becomes interdependent or depends critically on implementation
details not visible to potential derived classes.
A type that is both abstract and sealed should have only static members, and serves as what some
languages call a ―namespace‖ or ―static class‖. end rationale]

huangapple
  • 本文由 发表于 2023年6月2日 12:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定