#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

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.


About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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

  1. Pingback: Dew Drop – May 23, 2014 (#1783) | Morning Dew

  2. Pingback: Dew Drop – May 27, 2014 (#1784) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: