为什么 C# 没有非虚拟调用约定?

发布于 2024-09-12 10:37:18 字数 238 浏览 2 评论 0原文

受到这个问题:

为什么C#没有非虚拟调用约定的关键字?

编辑:我的意思是“既然 C# 编译器默认总是使用 IL 虚拟调用,为什么没有非虚拟 IL 调用的关键字”?

Inspired by this SO question:

Why doesn't C# have a keyword for non-virtual calling convention?

EDIT: I mean "why is there no keyword for non-virtual IL calls, given that the C# compiler always uses IL virtual calls by default"?

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

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

发布评论

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

评论(2

云朵有点甜 2024-09-19 10:37:18

看看 为什么 c# 默认将方法实现为非虚拟?

引用安德斯·海尔斯伯格的话

有几个原因。一是
表现。我们可以观察到
人们用 Java 编写代码,但他们忘记了
将他们的方法标记为最终的。
因此,这些方法是虚拟的。
因为它们是虚拟的,所以它们不
也执行。就只有
相关的性能开销
是一个虚拟方法。那是一个
问题。

一个更重要的问题是版本控制。
有两种思想流派
虚拟方法。学术学校
思想说:“一切都应该是
虚拟的,因为我可能想要
总有一天会推翻它。”
思想流派,源自
构建运行的真实应用程序
现实世界说:“我们必须
非常小心我们所做的事情
虚拟。”

当我们在
平台,我们做了很多
承诺如何发展
未来。对于非虚拟方法,我们
保证当你打电话时
方法,x和y将会发生。当我们
在 API 中发布虚拟方法,我们
不仅在您打电话时承诺
这个方法中,x和y都会发生。我们
还保证当你重写时
这个方法,我们将在this中调用它
特定顺序关于
这些其他的和国家将是
在这个和那个不变量中。

每次你在 API 中提到虚拟时,
您正在创建一个回调挂钩。作为
操作系统或 API 框架设计者,
你必须非常小心
那。您不希望用户覆盖
并在任意点进行挂钩
API,因为你不一定
做出这些承诺。人们可能
不完全理解他们的承诺
当他们做某事时正在做
虚拟。

来源:http://www.artima.com/intv/nonvirtual.html

Take a look at why c# implements methods as non-virtual by default?

To quote Anders Hejlsberg

There are several reasons. One is
performance. We can observe that as
people write code in Java, they forget
to mark their methods final.
Therefore, those methods are virtual.
Because they're virtual, they don't
perform as well. There's just
performance overhead associated with
being a virtual method. That's one
issue.

A more important issue is versioning.
There are two schools of thought about
virtual methods. The academic school
of thought says, "Everything should be
virtual, because I might want to
override it someday." The pragmatic
school of thought, which comes from
building real applications that run in
the real world, says, "We've got to be
real careful about what we make
virtual."

When we make something virtual in a
platform, we're making an awful lot of
promises about how it evolves in the
future. For a non-virtual method, we
promise that when you call this
method, x and y will happen. When we
publish a virtual method in an API, we
not only promise that when you call
this method, x and y will happen. We
also promise that when you override
this method, we will call it in this
particular sequence with regard to
these other ones and the state will be
in this and that invariant.

Every time you say virtual in an API,
you are creating a call back hook. As
an OS or API framework designer,
you've got to be real careful about
that. You don't want users overriding
and hooking at any arbitrary point in
an API, because you cannot necessarily
make those promises. And people may
not fully understand the promises they
are making when they make something
virtual.

Source: http://www.artima.com/intv/nonvirtual.html

听不够的曲调 2024-09-19 10:37:18

这可能会解释它: 链接

很快:call 指令可以接受 null 作为 this 指针(就像在 C++ 中一样)。这是错误的,C# 团队决定尽可能使用 callvirt ,以便对 null 指针的调用抛出 NullReferenceException

This might explain it: Link

Shortly: call instruction can accept null as this pointer (as it is in C++). This is errorsome and C# team decided to use callvirt wherever it is possible so that calls on null pointers throw NullReferenceException

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