#963 – Use Braces to Enclose a Block of Statements

block of statements in C# is a series of individual statements enclosed by a set of curly braces ({, }).  You can typically include a block of statements wherever a single statement is expected.

            // Single statement follows if
            if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
                Console.WriteLine("Getting to work");

            // Block of statements follows if
            if (DateTime.Now.DayOfWeek == DayOfWeek.Saturday)
            {
                Console.WriteLine("Making pancakes");
                Console.WriteLine("Washing my socks");
                Console.WriteLine("Mowing the lawn");
            }

Although far less common, you can also include a block of statements to arbitrarily group a set of statements within a longer sequence of statements.

            Console.WriteLine("Reading Edith Wharton");

            {
                Console.WriteLine("Eating lunch");
                Console.WriteLine("Washing my hands");
            }

            Console.WriteLine("Studying Byzantium");

963-001

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #963 – Use Braces to Enclose a Block of Statements

  1. Pingback: #1,103 – A Block Defines Both Scope and Declaration Space | 2,000 Things You Should Know About C#

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: