#964 – Declaring a Variable within a Block of Statements

When you declare a block of statements in your code, you can declare variables within that block that will have a scope that is local to the block.  This is, the variable will be visible only to other code within the block.

This is most often done within a function:

        static void DoSomething()
            // Variable local to this function
            string name = "Bob";


You can, however, declare local variables within any block of statements.

            if (DateTime.Now.DayOfWeek == DayOfWeek.Wednesday)
                string bestFood = "Pizza";

            // Can't access bestFood variable out here

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.

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: