Suppose you have this code in Unity (adapted from another SO question):
public class Test : MonoBehaviour {
void Start() {
int x = 0;
void foo() {
int x = 0;
x = 1;
Debug.Log(x);
}
}
}
In Visual Studio 2019, the compiler happily accepts this code, which makes sense given the explanation provided at the SO answer.
However Unity throws error CS0136, with the message:
error CS0136: A local or parameter named 'x' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
This seems to reference local scopes (not local functions), which indeed cannot redefine variables declared in the containing scope.
Local functions do seem to work in Unity (I can call foo after changing the variable names and get the expected Debug.Log output), so I'm wondering why this difference in compilers exists (and if there's acknowledgement/explanation for that difference somewhere in the docs).
Why does Unity throw an error here?
Unity Player Settings are set to Mono for Scripting backend and .NET Standard 2.0 for API compatibility (if that makes a difference).
EDIT: Unity version is 2019.3.0f6.