问题描述
有此事与同事友好的辩论。我们对此有一些想法,但想知道什么是SO人群认为这件事?
Having a friendly debate with a co-worker about this. We have some thoughts about this, but wondering what the SO crowd thinks about this?
推荐答案
原因之一是有一个只读本地没有CLR支持。 readonly是被翻译成CLR / CLI initonly运code。该标志只能应用于字段和具有用于本地没有意义。事实上,它应用到本地可能会产生无法核实code。
One reason is there is no CLR support for a readonly local. Readonly is translated into the CLR/CLI initonly opcode. This flag can only be applied to fields and has no meaning for a local. In fact, applying it to a local will likely produce unverifiable code.
这并不意味着C#不能做到这一点。但是,这将提供两个不同的含义相同的语言构造。该版本当地人就没有CLR等同映射。
This doesn't mean that C# couldn't do this. But it would give two different meanings to the same language construct. The version for locals would have no CLR equivalent mapping.
这篇关于为什么C#不允许只读局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!