see the code below:
public static int Main()
{
int j = 20;
for (int i=0; i < 10; i++)
{
int j = 30; //can't do this
Console.WriteLine(j + i);
}
return 0;
}
An error occurs in C#, because here it does not do variable hiding & gives error :
A local variable named 'j' cannot be declared in this scope because it would give a different meaning to 'j' which is already used in 'parent or current' scope.
if we run above program in c++, it works, in c++ it supports variable hiding in this situation.
Now my question is that , what is the reason behind this?
why C# developer decided to not support to hiding here.?
why C++ allow hiding here?