#1,104 – Can’t Re-Declare a Variable within a Nested Block

A declaration space is a region of code within which you can’t declare two entities having the same name.  

Defining a class creates a new declaration space, i.e. you can declare variables at the class level.

Defining a method creates a new declaration space, known as a local variable declaration space.  A block of code within that method creates a nested local variable declaration space.

It’s of course an error to declare two variables of the same name within a local variable declaration space.  It’s also an error to re-declare a variable of the same name in a nested local variable declaration space.

        public void MethodA()
        {
            int y = 1;
            string y = "oops";  // ERROR

            int x = 12;
            Console.WriteLine(12);

            if (DateTime.Now.DayOfWeek == DayOfWeek.Thursday)
            {
                // ERROR: Can't re-declare x within nested local variable declaration space
                double x = 4.2;
                Console.WriteLine(x);
            }
        }
Advertisement

#720 – Location of Declarations within A Method Does Matter

When declaring members in a class, you can declare the members anywhere in the class, even placing a declaration after some code that references the declared member.

When you declare variables within a method, however, a variable must be declared before it is used.  Compiling the code below will result in a compile-time error, since the leadin variable in the method RevealNames is declared after it is used.

    public class Dog
    {
        public string Name { get; set; }

        public Dog(string name)
        {
            Name = name;
            secretName = "His Great Terribleness Mr. Bites-a-Lot";
        }

        public void RevealNames()
        {
            Console.WriteLine(leadin);
            Console.WriteLine(string.Format("{0} is really {1}", Name, secretName));
            string leadin = "Did you know?";
        }

        private string secretName;
    }

#719 – Location of Declarations within A Class Doesn’t Matter

Within the code for a class, you can declare members either before or after they are used.

In the example below, the Name property is declared earlier in the class than it is used.  But the secretName field is declared after it is used.

    public class Dog
    {
        public string Name { get; set; }

        public Dog(string name)
        {
            Name = name;
            secretName = "His Great Terribleness Mr. Bites-a-Lot";
        }

        public void RevealNames()
        {
            Console.WriteLine(string.Format("{0} is really {1}", Name, secretName));
        }

        private string secretName;
    }

#708 – A Name Must Be Unique Within A Declaration Space

When you declare an element in C#, your declaration exists within a particular declaration space.  The declaration space is the context within which you can’t have two elements declared with the same name.

One example of a declaration space is a class–within a class, you cannot have more than one member with the same name, but you can have a member with the same name in a different class.  In the example below, we define a Name property in one class and a Name method in another class.

    public class Dog
    {
        public string Name { get; set; }
        public int Age { get; set; }

        // NO: We already have a "Name" member in this declaration space
        //public void Name()
        //{
        //}
    }

    public class Baby
    {
        public string BabyName { get; set; }

        public void Name(string newName)
        {
            BabyName = string.Format("Cute little {0}", newName);
        }
    }

#510 – Declaring More than One Local Variable On the Same Line

You typically declare a local variable by specifying its type, the name of the local variable and an optional initial value.

int j;
int i = 42;

You can declare more than one variable on the same line, as long as the variables all have the same type.  Each variable can optionally include an initialization.

int j, i = 42;      // Only i is assigned
string name = "Sean", motto = "Carpe Libri";
Dog d1, d2, d3;
Dog kirby = new Dog("Kirby", 12), jack = new Dog("Jack", 14);
dynamic thing1, thing2;

You can’t, however, declare multiple implicitly-typed variables on the same line.

            var i = 12, s = "Sean";   // Error: Implicitly-typed local variables cannot have multiple declarators