为什么 C# 不提供类似于 C++ 的常量性?
C# 中的引用与 C++ 中的引用非常相似,只不过它们是垃圾收集的。
为什么 C# 编译器支持以下内容如此困难:
- 标记为
const
的成员函数。 - 对标记为
const
的数据类型(字符串除外)的引用,通过它只能调用const
成员函数?
我相信如果 C# 支持这一点将会非常有用。 其一,它确实有助于 C# 程序员返回对私有数据的赤裸裸引用的看似普遍存在的同性恋放弃(至少我在工作场所看到的是这样的)。
或者 C# 中是否已经有我所缺少的等效内容? (我知道 readonly
和 const
关键字,但它们并不能真正达到上述目的)
References in C# are quite similar to those on C++, except that they are garbage collected.
Why is it then so difficult for the C# compiler to support the following:
- Members functions marked
const
. - References to data types (other than string) marked
const
, through which onlyconst
member functions can be called ?
I believe it would be really useful if C# supported this. For one, it'll really help the seemingly widespread gay abandon with which C# programmers return naked references to private data (at least that's what I've seen at my workplace).
Or is there already something equivalent in C# which I'm missing? (I know about the readonly
and const
keywords, but they don't really serve the above purpose)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我怀疑有一些实际原因和一些理论原因:
不过,现在无法支持它的原因有一个很大的原因:
我同意它会拥有某种常量指示器是有用的,但恐怕我看不到它发生。
编辑:多年来,Java 社区对此一直存在争论,有很多关于 相关错误,您可能会感兴趣。
I suspect there are some practical reasons, and some theoretical reasons:
There's a big one in terms of why it can't be supported now though:
I agree it would be useful to have some sort of constness indicator, but I can't see it happening, I'm afraid.
EDIT: There's been an argument about this raging in the Java community for ages. There's rather a lot of commentary on the relevant bug which you may find interesting.
正如 Jon 已经介绍过的(当然)const 正确性并不像看起来那么简单。 C++ 以一种方式实现这一点。 D 采用另一种(可以说更正确/有用)的方式。 正如您所发现的那样,C# 对此进行了调情,但没有做任何更大胆的事情(并且可能永远不会很好,正如 Jon 再次详细介绍的那样)。
也就是说,我相信 Jon 的许多“理论原因”在 D 的模型中得到了解决。
在 D (2.0) 中,const 的工作方式与 C++ 非常相似,只是它是完全传递的(因此应用于指针的 const 将应用于指向的对象、该对象的任何成员、该对象拥有的任何指针、它们指向的对象等) - 但很明显,这仅适用于您声明为 const 的变量(因此,如果您已经有一个非常量对象并且您使用一个指向它的 const 指针,则该非常量变量仍然可以改变状态)。
D 引入了另一个关键字 - invariant - 它适用于对象本身。 这意味着一旦初始化,没有任何东西可以改变状态。
这种安排的优点在于 const 方法可以接受 const 对象和不变对象。 由于不变对象是函数世界的面包和黄油,并且 const 方法可以在函数意义上被标记为“纯粹”——即使它可以与可变对象一起使用。
回到正轨 - 我认为我们现在才(顽皮的后半部分)了解如何最好地使用 const (和不变量)。 .Net 最初是在事情还比较模糊的时候定义的,所以没有承诺太多——现在改造已经太晚了。
不过,我希望看到 D 端口在 .Net VM 上运行:-)
As Jon already covered (of course) const correctness is not as simple as it might appear. C++ does it one way. D does it another (arguably more correct/ useful) way. C# flirts with it but doesn't do anything more daring, as you have discovered (and likely never well, as Jon well covered again).
That said, I believe that many of Jon's "theoretical reasons" are resolved in D's model.
In D (2.0), const works much like C++, except that it is fully transitive (so const applied to a pointer would apply to the object pointed to, any members of that object, any pointers that object had, objects they pointed to etc) - but it is explicit that this only applies from the variable that you have declared const (so if you already have a non-const object and you take a const pointer to it, the non-const variable can still mutate the state).
D introduces another keyword - invariant - which applies to the object itself. This means that nothing can ever change the state once initialised.
The beauty of this arrangement is that a const method can accept both const and invariant objects. Since invariant objects are the bread and butter of the functional world, and const method can be marked as "pure" in the functional sense - even though it may be used with mutable objects.
Getting back on track - I think it's the case that we're only now (latter half of the naughties) understanding how best to use const (and invariant). .Net was originally defined when things were more hazy, so didn't commit to too much - and now it's too late to retrofit.
I'd love to see a port of D run on the .Net VM, though :-)
先生。 C#语言的设计者Heljsberg已经回答了这个问题:
http://www .artima.com/intv/choicesP.html
Mr. Heljsberg, the designer of the C# language has already answered this question:
http://www.artima.com/intv/choicesP.html
如果将不可变类型添加到 C# 的未来版本中,我不会感到惊讶。
C# 3.0 已经朝着这个方向迈出了一步。
例如,匿名类型是不可变的。
我认为,由于旨在拥抱并行性的扩展,您可能会看到越来越多的不变性出现。
I wouldn't be surprised if immutable types were added to a future version of C#.
There have already been moves in that direction with C# 3.0.
Anonymous types, for example, are immutable.
I think, as a result of extensions designed to embrace parallelism, you will be likely to see immutability pop up more and more.
问题是,我们在 C# 中需要常量吗?
我非常确定 JITter 知道给定的方法不会影响对象本身并自动执行相应的优化。 (也许通过发出
call
而不是callvirt
?)我不确定我们是否需要这些,因为constness的大多数优点都与性能相关,你最终会在第 1 点。
除此之外,C# 还具有
readonly
关键字。The question is, do we need constness in C#?
I'm pretty sure that the JITter knows that the given method is not going to affect the object itself and performs corresponding optimizations automagically. (maybe by emitting
call
instead ofcallvirt
?)I'm not sure we need those, since most of the pros of constness are performance related, you end up at the point 1.
Besides that, C# has the
readonly
keyword.