C# 中关于覆盖的类型签名差异

发布于 2024-10-06 23:26:28 字数 636 浏览 5 评论 0原文

这次关于方差的问题略有不同。

我从实验中得知,C# 不允许用逆变函数覆盖虚函数?如果是这样,你会怎么做,因为我不断收到“找不到合适的方法重载”的消息。如果不是,为什么不呢?如果基类指定in的值永远是int,而子类可以取一个对象,问题出在哪里?

与返回类型相同,其重写签名应该是协变的

什么语言允许您执行此操作? (C++/Java 我正在看着你)如果他们让你使用,方差是什么类型? (返回类型/参数列表等)

您可以有多个覆盖吗?我不明白为什么不只要签名在技术上是唯一的。

为什么 C#/CLR 不支持方法重写 co /contra-variance?

我读了这篇文章,但没有看到任何问题的答案。我了解通用方差,而且我知道它很棒。我想了解方法签名方差,不仅在 C# 中,而且在 C# 中(即包括但不限于)。如果它不存在,我想要对此进行确认并进行解释(是实现细节吗?我的类型理论是否错误?是否只是 CLR/C# 碰巧不支持此特定功能?这是我没有的东西吗?没有想到,因为我不需要处理 CLR 的实现,因此不知道具体的复杂细节?)

slightly different question about variance this time.

I take it from experimentation that C# does not allow you to override a virtual function with a contravariant function? If it does, how do you do it, since I keep getting "suitable method overload not found." If it isn't, why not? if the base class specifies that the value in will always be an int, and the sub-class can take an object, where is the problem?

Same to return types, should be covariant in their overrride signature

What languages allow you to do this? (C++/Java I am looking at you) and if they let you use, variance what types? (return type/argument list etc.)

Could you have multiple overrides? I don't see why not as long as the signatures were technically unique.

Why does C#/CLR not support method override co/contra-variance?

I read this and I see no answers to any of the questions. I know about generic variance, and I know it's awesome. I want to know about method signature variance, and not just in C#, but also in C#(i.e. including but not limited to). If it doesn't exist I would like a confirmation of this and an explanation(is it an implementation detail? is my type theory wrong? Is it just the CLR/C# happens to not support this particular feature? Is it something I haven't thought of since I don't have to deal with implementing the CLR, and therefore don't know specific complex details?)

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

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

发布评论

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

评论(1

ぃ弥猫深巷。 2024-10-13 23:26:28

找到了答案,C# 规范中的第 10.6.4 节规定重写必须具有相同的签名

Found the answer, section 10.6.4 in the C# specification states that overrides must have the same signature

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