1

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?

dbc
  • 104,963
  • 20
  • 228
  • 340
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • Why isn't it supported? Because it's an unnecessary requirement, when designing a new language. – Mark Ingram Nov 15 '12 at 13:51
  • 1
    A philosophical point: C++ always treats the programmer like she's fully aware of what she's doing, and if that is something weird and despicable, it won't get in your way. C# may be taking a more benevolent-parent standpoint and try harder to keep you out of danger. In C++, the compiler is your best friend, in C# it's the language itself. – Kerrek SB Nov 15 '12 at 13:54
  • Related: [C# variable scoping: 'x' cannot be declared in this scope because it would give a different meaning to 'x'](https://stackoverflow.com/q/32254127). – dbc May 12 '19 at 06:56

3 Answers3

3

C++ kept things like this for compatibility with C. This was allowed in C, so it's allowed in C++.

C# doesn't have nor need compatibility. So this "hiding" was weeded out.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

The answer in such things is always the same: because nobody has evaluated, specified, designed, implemented, tested, documented, communicated, translated and supported that feature. In this case, I would imagine simply because there is no tangible benefit (you can just use a different variable name, which then avoids all issues of ambiguity), and there was no pre-existing need to have the feature for legacy reasons.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

The rationale is that it is considered to be potentially confusing and unnecessary to have two variables in the same function.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • They did allow you to hide a variable one level higher at the class level though! – René Wolferink Nov 15 '12 at 13:56
  • 1
    @RenéWolferink: Yes, that is allowed, but those are completely different levels. If you don't allow that, then you cannot *freely* add new member attributes to a class during maintenance, as those might collide with existing local variables. That is, when designing a language you have to balance the cost and the value: inside a function the cost is minimal, with a member the cost is potentially having to rewrite the body of member functions when adding a new member variable --or forcing the choice of a different potentially *worse* name for the new member [...] – David Rodríguez - dribeas Nov 15 '12 at 14:02
  • 1
    [...] Add to that partial classes and extension methods and the compiler would have to play a horrible dance to verify that the new member does not collide with anything else. – David Rodríguez - dribeas Nov 15 '12 at 14:03
  • @DavidRodríguez-dribeas:if it is unnecessary then why C++ developer not upgraded or removed it – Ravindra Bagale Nov 15 '12 at 14:10
  • @RavindraBagale: Backwards compatibility. Changing this would break existing code in C++ and would further break compatibility with C that also allows it. – David Rodríguez - dribeas Nov 15 '12 at 18:17