What I am showing below, is rather a theoretical question. But I am interested in how the new C#7 compiler works and resolves local functions.
In C#7
Parameters and local variables from the enclosing scope are available inside a local function.
I thought, each inner Main() would have a local scope and is hidden outside.
C# does not overwrite names from the parent scope, so there is and ambiguity for the local name Main
which defined in current and parent scopes.
So in the second example both declaration of void Main()
are available for the inner scope and compiler shows you an error.
Here is an example with variables and local functions
which could help you to see the problem in the familiar environment. To make it clear that it is only matter of scope I modified example and added functions to variables to make it clear:
class Test
{
int MainVar = 0;
public void Main()
{
if (this.MainVar++ > 10) return;
int MainVar = 10;
Console.WriteLine($"Instance Main, this.MainVar=${this.MainVar}, MainVar={MainVar}");
void Main()
{
if (MainVar++ > 14) return;
Console.WriteLine($"Local Main, this.MainVar=${this.MainVar}, MainVar={MainVar}");
// Here is a recursion you were looking for, in Example 1
this.Main();
// Let's try some errors!
int MainVar = 110; /* Error! Local MainVar is already declared in a parent scope.
// Error CS0136 A local or parameter named 'MainVar' cannot be declared in this scope
// because that name is used in an enclosing local scope to define a local or parameter */
void Main() { } /* Error! The same problem with Main available on the parent scope.
// Error CS0136 A local or parameter named 'Main' cannot be declared in this scope
// because that name is used in an enclosing local scope to define a local or parameter */
}
Main(); // Local Main()
this.Main(); // Instance Main()
// You can have another instance method with a different parameters
this.Main(99);
// But you can't have a local function with the same name and parameters do not matter
void Main(int y) { } // Error! Error CS0128 A local variable or function named 'Main' is already defined in this scope
}
void Main(int x)
{
Console.WriteLine($"Another Main but with a different parameter x={x}");
}
}
There are even the same errors when you try to overwrite local variable and local function.
So as you can see it is a matter of scopes and you cannot overwrite local function or variable.
BTW, in a first example you could make recursive call by using this.Main();
:
void Main()
{
void Main()
{
Console.WriteLine("Hello!");
}
this.Main(); // call instance method
}
Footnote: Local functions are not represented as delegates as some commentators suggest and it makes local functions
much leaner in both memory and CPU.
As c# is a statically compiled language, i'd think all the functions are compiled before their encompassing scope is executed and therefor the innermost Main
cannot be declared because a Main
already exists from its' closure point of view (one level up).
Note that this is not based on factual evidence but just my initial thoughts on the matter.
After some research... I was wrong, mostly. My gut feeling does seem to imply the same behaviour, But not for the reasons I initially thought.
@PetSerAl already explained in comment form better than I could have copied it from manuals so I defer to that answer.
To expand a bit on v-andrew's answer, it is indeed analogous to having two variables with the same name. Consider that the following is allowed:
void Main()
{
{
void Main()
{
Console.WriteLine("Hello!");
}
Main();
}
{
void Main()
{
Console.WriteLine("GoodBye!");
}
Main();
}
}
Here we've two scopes and so we can have two local functions of the same name in the same method.
Also to combine v-andrew's answer and your question, note that you can (and always could) have a variable called Main
inside Main()
but you can't have both a variable and a local function of the same name in the same scope either.
On the other hand, you can't overload locals like you can members by having different parameters.
Really, it's all closer to the existing rules for locals than the existing rules for methods. Indeed, it's the same rules. Consider you can't do:
void Main()
{
{
void Main()
{
int Main = 3;
Console.WriteLine(Main);
}
Main();
}
}
I thought, each inner Main() would have a local scope and is hidden outside.
It is, but the scope includes the name of the local function. C.f. that you can't redefine a variable name from a for
, foreach
or using
inside the its scope.
Meanwhile, I think it is a compiler bug.
It's a compiler feature.
That means, removing the L from MainL should not harm because the compiler already renames it in a unique way, it should result in IL code like.
That means it's possible to introduce a bug into the compiler where the code you have in your question would work. That would be in violation to the C# rules for names of locals.
it is confusing in C#, but logical in C++
It blocks something that has been known as a source of mistakes for some time. Likewise in C# you are not allowed to use integer values with if()
and you have to explicitly fall-through in switch
statements. All of these are changes that C# made to how it compares to C++ in the very beginning and all of them remove some convenience, but all of them are things that people really had found caused bugs and often prohibited in coding conventions.
Since Stackoverflow does not allow multiple answers, I thought what would be the fairest way. I created this answer as community wiki (so I won't get any rep points for this answer), upvoted the two answers below and added them as link for your reference (so they are honored and get rep points for their answers individually):
And I created a summary in the question containing all information I got from you from the comments and from the answers: