为什么C#不允许只读局部变量? [英] Why does C# disallow readonly local variables?

查看:98
本文介绍了为什么C#不允许只读局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有此事与同事友好的辩论。我们对此有一些想法,但想知道什么是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#不允许只读局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆