Why are assignment operators (=) invalid in a foreach loop?

后端 未结 10 1247
栀梦
栀梦 2020-12-14 07:10

Why are assignment operators (=) invalid in a foreach loop? I\'m using C#, but I would assume that the argument is the same for other languages that support

10条回答
  •  误落风尘
    2020-12-14 07:59

    It would be perfectly possible to let it be altered. However, what does this then mean? It would read like the underlying enumeration was modified, which it isn't (it would be possible to allow that too, but that has its own downsides).

    So you'd have code that people would naturally read as indicating something other than what has actually happened. Considering that the purpose of a computer language is primarily to be understood by people (compilers deal with the balance being set against them, unless you use assembly, machine code, or the appropriately named Brainf**k) this would indicate a flaw in the language.

提交回复
热议问题