3

Possible Duplicate:
Why can't a duplicate variable name be declared in a nested local scope?

I am new to C# and recently noticed that scoped objects in C# when declared within two different scopes can't have the same name as in Java. Why is this design limitation placed? Any ideas? E.g. the following code will not work in C#,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        private string Name = "";
        public Program(string Name)
        {
            this.Name = Name;
        }

        public static void Main(string[] args)
        {
            bool flag = true;
            if (flag)
            {
                Program p = new Program("");
                return;
            }

            Program p = new Program("");
            return;
        }
    }
}

The 24th line of the code where the second instance of class Program is created in terms of reference p creates a compile time error. Whereas the exact same code runs in Java without any problem,

public final class Test {
    private String Name = "";
    public Test (String Name) {
        this.Name = Name;
    }

    public static void main(String[] args) {
        boolean flag = true;
        if (flag)
        {
            Test t = new Test("");
            return;
        }

        Test t = new Test("");
        return;
    }
}

If I remember from my compiler class: as the control flow of the inner nested scope (within the if loop) is independent of the part of the main below it, there is no harm at all in allowing the user to use the same variable name reference (as done by the Java compiler). It should not be a compile time error. So is there a mystery in C# having such a design restriction?

Community
  • 1
  • 1
user396089
  • 1,115
  • 1
  • 12
  • 26
  • "Because this is how the C# language is defined". Scala (and Java ;-) allows this, for instance, so there is nothing inherently preventing it from being valid except it was deemed as an illegal construct by some language author somewhere ;-) –  Jun 01 '12 at 03:24
  • 7
    http://blogs.msdn.com/b/ericlippert/archive/2009/11/02/simple-names-are-not-so-simple.aspx – Anthony Pegram Jun 01 '12 at 03:27
  • 2
    @AnthonyPegram Very nice read. Consider posting that as an answer (*with* the relevant excerpts). –  Jun 01 '12 at 03:30
  • 6
    See Jon Skeet's answer here: http://stackoverflow.com/questions/6156449/a-local-variable-cannot-be-declared-in-this-scope – McAden Jun 01 '12 at 03:35
  • Ok, I got it. It seems like a quirk of the language designers, which is fine (personal preference). I was more hoping that it might have something to do with compiler theory. – user396089 Jun 01 '12 at 04:33
  • You may wanna see [C# language specification](http://www.microsoft.com/en-us/download/details.aspx?id=7029) and section 3.3 Declaration. – Habib Jun 01 '12 at 04:35
  • but this is fine `{Program p = new Program("");}{Program p = new Program("");}` – Damith Jun 01 '12 at 04:56

0 Answers0