且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么C#不允许只读局部变量?

更新时间:2023-11-09 19:44:04

原因之一是有一个只读本地没有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.