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 8906989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:47:22+00:00 2026-06-15T02:47:22+00:00

Let’s take the following code: class Foo { string bar; public void Method() {

  • 0

Let’s take the following code:

class Foo
{
   string bar;

   public void Method()
   {
      if (!String.IsNullOrEmpty(this.bar))
      {
         string bar = "Hello";
         Console.Write(bar);
      }
   }
}

This will compile, and all is good. However, let’s now remove the this. prefix:

class Foo
{
   string bar;

   public void Method()
   {
      if (!String.IsNullOrEmpty(bar)) // <-- Removed "this."
      {
         string bar = "Hello";
         Console.Write(bar);
      }
   }
}

In this case, I get a compiler error. I agree this is an error, however it’s the location of the error that confuses me. The error happens on the line:

string bar = "Hello";

With the message:

A local variable named ‘bar’ cannot be declared in this scope because
it would give a different meaning to ‘bar’, which is already used in a
‘parent or current’ scope to denote something else

From what I understand about the compiler, the declaration of bar gets hoisted to the top of the Method() method. However, if that’s the case, the line:

if (!String.IsNullOrEmpty(bar))

Should be considered ambiguous, as bar could be a reference to the instance field or to the not-yet-declared local variable.

To me, it seems odd that removing this. can cause a compilation error on another line. In other words, declaring a local bar variable is perfectly valid, as long as no potentially ambiguous references to bar have been made previously in the scope (note, if I comment out if (!String.IsNullOrEmpty(bar)) then the error goes away).

That all seems rather pedantic, so what’s your question?:

My question is why the compiler allows an ambiguous reference to a variable before it’s declared in the scope, but then flags the declaration itself as redundant. Shouldn’t the ambiguous reference to bar in String.IsNullOrEmpty() be a more precise location of the error? In my example, it’s of course easy to spot, but when I ran across this issue in the wild, the reference was pages up and much harder to track down.

  • 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-06-15T02:47:24+00:00Added an answer on June 15, 2026 at 2:47 am

    From what I understand about the compiler, the declaration of bar gets hoisted to the top of the Method() method.

    No, that’s not the case.

    The error message is quite precise here:

    A local variable named ‘bar’ cannot be declared in this scope because it would give a different meaning to ‘bar’, which is already used in a ‘parent or current’ scope to denote something else.

    The part of the C# spec which is being violated is section 7.6.2.1 (C# 4 and 5 specs):

    Invariant meaning in blocks
    For each occurrence of a given identifier as a full simple-name (without a type argument list) in an expression or declarator, within the local variable declaration space (§3.3) immediately enclosing that occurrence, every other occurrence of the same identifier as a full simple-name in an expression or declarator must refer to the same entity. This rule ensures that the meaning of a name is always the same within a given block, switch block, for-, foreach- or using-statement, or anonymous function.

    And in the annotated C# specification, this has a helpful annotation from Eric Lippert:

    One of the more subtle desirable consequences of this rule is that it becomes safer to undertake refactorings that involve moving around local variable declarations. Any such refactoring that would cause a simple name to change its semantics will be caught by the compiler.

    Aside from anything else though, it seems to me that this is just good for clarity. Even if the second version were allowed, the first is clearer IMO. The compiler is ensuring that you don’t write pathologically unclear code, when you can very easily fix it to be obvious what you mean where.

    To put it another way: do you really want to be able to write the second version?

    In particular:

    In my example, it’s of course easy to spot, but when I ran across this issue in the wild, the reference was pages up and much harder to track down.

    … and that makes it more reasonable to allow it? Quite the reverse, I’d say – and you should also treat it as strong encouragement to refactor your code so that a single method isn’t “pages” long.

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

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say that I have classes like this: public class Parent { public int
Let's say I have the following piece of code. To test this, I change
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I can call a method like this: core::get() . What is the
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress

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.