【发布时间】:2020-01-16 08:28:29
【问题描述】:
在 C# 规范的 8.8.4 中,它提供了这个例子:
形式的foreach语句
foreach (V v in x) embedded-statement
然后扩展为:
{
E e = ((C)(x)).GetEnumerator();
try {
V v;
while (e.MoveNext()) {
v = (V)(T)e.Current;
embedded-statement
}
}
finally {
… // Dispose e
}
}
它还说:
迭代变量对应一个只读局部变量 扩展嵌入语句的范围。
变量v在嵌入语句中是只读的。
如何将迭代变量设为只读?
在 C# 中你不能在这里使用 readonly,而且 const 也不起作用。
这是我做的一个例子。
我查看了 CIL 代码,但看不到它使迭代变量只读的任何地方:
C#:
class Program
{
static void Main(string[] args)
{
var enumerable = new List<string> { "a", "b" };
foreach (string item in enumerable)
{
string x = item;
}
}
}
CIL:
.method private hidebysig static
void Main (
string[] args
) cil managed
{
// Method begins at RVA 0x2050
// Code size 80 (0x50)
.maxstack 3
.entrypoint
.locals init (
[0] class [mscorlib]System.Collections.Generic.List`1<string> enumerable,
[1] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<string>,
[2] string item,
[3] string x
)
IL_0000: nop
IL_0001: newobj instance void class [mscorlib]System.Collections.Generic.List`1<string>::.ctor()
IL_0006: dup
IL_0007: ldstr "a"
IL_000c: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)
IL_0011: nop
IL_0012: dup
IL_0013: ldstr "b"
IL_0018: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)
IL_001d: nop
IL_001e: stloc.0
IL_001f: nop
IL_0020: ldloc.0
IL_0021: callvirt instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<string>::GetEnumerator()
IL_0026: stloc.1
.try
{
IL_0027: br.s IL_0035
// loop start (head: IL_0035)
IL_0029: ldloca.s 1
IL_002b: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::get_Current()
IL_0030: stloc.2
IL_0031: nop
IL_0032: ldloc.2
IL_0033: stloc.3
IL_0034: nop
IL_0035: ldloca.s 1
IL_0037: call instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::MoveNext()
IL_003c: brtrue.s IL_0029
// end loop
IL_003e: leave.s IL_004f
} // end .try
finally
{
IL_0040: ldloca.s 1
IL_0042: constrained. valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<string>
IL_0048: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_004d: nop
IL_004e: endfinally
} // end handler
IL_004f: ret
} // end of method Program::Main
【问题讨论】:
-
看起来它只是执行该规则的编译器。如果您手动降低 foreach 以使用 Enumerator,您可以重新分配所有您想要的。
标签: c# foreach iterator ienumerable cil