为什么 C# 扩展方法必须在静态类中定义?

发布于 2024-12-01 03:59:59 字数 82 浏览 8 评论 0原文

我知道 C# 扩展方法必须是静态的。我不明白的是为什么这些扩展不能在非静态类或通用类中定义?

更新:我对这个设计决定背后的原因感兴趣。

I understand that C# extension methods must be static. What I don't understand is why these extensions can't be defined in non static classes or generic ones?

Update: I am interested in the reason behind this design decision.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

眉黛浅 2024-12-08 03:59:59

这更多的是一种观察而不是答案,但是......

当您调用实例方法时,对您正在调用的对象的引用将作为方法调用中的第一个参数推送到堆栈上。第一个参数是“this”并且是隐式完成的。

当您定义扩展方法时,您显式地将“this”定义为第一个参数。

如果您可以在同一个类中定义扩展方法和实例方法,即定义具有相同名称的方法,并且实际上在“this”参数时具有相同的参数,那么方法解析是否可能会令人困惑包括在内。

This is more of an observation than an answer, but...

When you call an instance method, a reference to the object you are calling is pushed onto the stack as the first argument in your method call. That first argument is "this" and is done implicitly.

When you define an extension method, you explicitly define a "this" as the first argument.

Is it possible that method resolution would be confusing if you could define extension methods and instance methods in the same class i.e. defining methods with the same name and, in effect, the same parameters when the "this" parameter is included.

早乙女 2024-12-08 03:59:59

看一下 .NET C# 规范的这一部分:

当方法的第一个参数包含 this 修饰符时,
方法被称为扩展方法。扩展方法只能
在非泛型、非嵌套静态类中声明。第一个
扩展方法的参数不能有除
this,并且参数类型不能是指针类型。

这个片段来自 Jon Skeet 的回答

我不清楚为什么所有这些限制都是必要的 -
除了潜在的编译器(和语言规范)简单性之外。我
可以明白为什么将其限制为非泛型类型是有意义的,但我
无法立即明白为什么它们必须是非嵌套的和静态的。我
怀疑如果你不这样做的话,它会使查找规则变得相当简单
必须担心当前类型等中包含的类型,但我
敢说这是可能的。

Take a look to this piece of the .NET C# specification:

When the first parameter of a method includes the this modifier, that
method is said to be an extension method. Extension methods can only
be declared in non-generic, non-nested static classes. The first
parameter of an extension method can have no modifiers other than
this, and the parameter type cannot be a pointer type.

And this fragment from Jon Skeet's answer:

It's not clear to me why all of these restrictions are necessary -
other than potentially for compiler (and language spec) simplicity. I
can see why it makes sense to restrict it to non-generic types, but I
can't immediately see why they have to be non-nested and static. I
suspect it makes the lookup rules considerably simpler if you don't
have to worry about types contained within the current type etc, but I
dare say it would be possible.

萌吟 2024-12-08 03:59:59

因为规范是这么说的……现在他们这样编写规范可能有充分的理由。

它们不能在泛型类中声明的原因非常明显:考虑到扩展方法的调用方式,您将在哪里指定类的类型参数?

为什么它必须是静态类的原因不太明显,但我认为这是有道理的。静态类的主要用例是将辅助方法组合在一起(例如 PathDirectoryProtectedData...),扩展方法是基本上是辅助方法。例如,能够创建 EnumerableQueryable 的实例是没有意义的。

Because the spec says so... Now there are probably good reasons why they wrote the spec this way.

The reason why they can't be declared in generic classes is quite obvious: given the way extension methods are called, where would you specify the type argument for the class?

The reason why it must be a static class is less obvious, but I think it makes sense. The main use case for static classes is to group helper methods together (e.g. Path, Directory, ProtectedData...), and extension methods are basically helper methods. It wouldn't make sense to be able to create an instance of Enumerable or Queryable, for example.

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