This piece of code produces the error CS0136:
static void Main(string[] args)
{
foreach (var arg in args) Console.WriteLine(arg); // error in this line (arg is underlined red)
string arg = "lol";
Console.WriteLine(arg);
}
Error description according to microsoft:
A local variable named 'var' cannot be declared in this scope because it would give a different meaning to 'var', which is already used in a 'parent or current/child' scope to denote something else.
In this case the compiler Claims that the parameter arg in the foreach loop is already declared. The Problem i have with this is , that i declare the string arg after the foreach loop, so it should work, or am i missing some point. Is some sort of "Optimizing" which rearranges the order of my code?
I understand, that if i would declare it before the foreach loop it would be an error. Furthermore i know that i could name it diffently. But i want to understand it.