Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:59:31+00:00 2026-05-29T22:59:31+00:00

C# is quite nitpicky when it comes to variable scoping. How is it possible

  • 0

C# is quite nitpicky when it comes to variable scoping. How is it possible that it accepts the following code?

class Program
{
    int x = 0;

    void foo()
    {
        int x = 0;
        x = 1;
        
        Console.WriteLine(x);
    }
}

If you ask me, that’s an obvious naming conflict. Still the compiler (Visual Studio 2010) accepts it. Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:59:34+00:00Added an answer on May 29, 2026 at 10:59 pm

    The rules for C# name hiding are quite complex. The language allows the case you mention, but disallows many similar cases. See

    Simple names are not so simple, part one

    for some information on this complicated subject.

    To address your specific question: the compiler certainly could detect that conflict. In fact, it does detect that conflict:

    class P
    {
        int x;
        void M()
        {
            x = 123; // The author intends "this.x = 123;"
            int x = 0;
        }
    }
    

    The equivalent C++ program would be legal C++, because in C++ a local variable comes into scope at the point of its declaration. In C#, a local variable is in scope throughout its entire block, and using it before its declaration is illegal. If you try compiling this program you get:

    error CS0844: Cannot use local variable 'x' before it is declared.
    The declaration of the local variable hides the field 'P.x'.
    

    See: the local declaration hides the field. The compiler knows it. So why in your case is it not an error to hide a field?

    Let’s suppose for the sake of argument that it should be an error. Should this also be an error?

    class B
    {
        protected int x;
    }
    
    class D : B
    {
        void M()
        {
            int x;
        }
    }
    

    The field x is a member of D via inheritance from B. So this should also be an error, right?

    Now suppose you have this program produced by Foo Corporation:

    class B
    {
    }
    

    and this program produced by Bar Corporation:

    class D : B
    {
        void M()
        {
            int x;
        }
    }
    

    That compiles. Now suppose Foo Corp updates their base class and ships a new version out to you:

    class B
    {
        protected int x;
    }
    

    You’re telling me that every derived class that contains a local variable named x should now fail to compile?

    That would be horrid. We have to allow local variables to shadow members.

    And if we’re going to allow locals to shadow members of base classes, it would seem awfully strange to not allow locals to shadow members of classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Case Quite often I find myself staring at some old code that doesn't look
Quite often in my code I need to compare a variable to several values
Quite often I have unit tests using MSTest for code that is in a
Quite often in ANSI C code I can see parenthesis sorrounding a single return
Quite new to functional languages, but I'm maintaining someone else's code with a lot
Quite a simple question. Is it possible to notice vibrations in the Android emulator?
Quite often I see source code where language's keyword are replaced with full type
Quite simply I'd like to print out all variables that are in scope in
Quite often in my LINQ to SQL code, I need to find or create an entity
Quite by accident today, I discovered that in IE6, IE7 and IE8 it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.