#1,103 – A Block Defines Both Scope and Declaration Space

  • Scope is the region of code in which you can refer to a named entity using its unqualified name.
  • Declaration space is a region of code within which you can’t declare two entities having the same name

A block of statements defines both a new scope and a new declaration space.

In the code below, the variable msg is declared within the block of statements following the if statement.

    public class MyClass
    {
        public void AMethod()
        {
            if (DateTime.Now.DayOfWeek == DayOfWeek.Thursday)
            {
                // Can define variables within this block
                string msg = "Hi";
                Console.WriteLine("{0}, it's Thursday. ", msg);
            }

            // ERROR: The name 'msg' does not exist in the current context
            Console.WriteLine(msg);
        }
    }

The block defines a new scope–the variable msg can be referred to by name anywhere within the block (after the declaration).  This block also defines a declaration space–you can only declare one variable named msg within this block.

Advertisements

#1,102 – Scope vs. Declaration Space

The terms scope and declaration space are similar, but slightly different.

  • Scope is the region of code in which you can refer to a named entity using its unqualified name.
  • Declaration space is a region of code within which you can’t declare two entities having the same name

For example:

    public class MyClass
    {
        int count1 = 1;
        int count2 = 2;

        public void AMethod()
        {
            int count2 = 22;

            Console.WriteLine(count1 + ", " + count2);
        }
    }

Then:

  • The class-level declarations of count1 and count2 are in the same scope and in the same declaration space
  • The body of function AMethod()
    • is included in the scope in which class-level fields are defined, i.e. you can reference them using their unqualified name.
    • defines a new declaration space, i.e. we can define a new variable of the same name as the class-level fields
    • defines a new scope, in which we can define variables, nested within outer scope

1102-001

#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);
        }
    }