Okay, I know you think I’ve seriously hit the crack pipe here. Curly Braces, as I’m sure we all know, are fundamental to programming in most languages. This is true no more so then here in C#. We use them for if, for, using, do, while, namespace scope, class scope, method scope and getters and setters for parameters.

So how could I dare to say the {} is an often unused operator? Well, I’m specifically talking about a use of the operators in this case, not the operators themselves.

Did you know you can use curly braces by themselves? Take a look at this:

public Window1()
{
    InitializeComponent();

    {
        int [] intList = { 1, 2, 3, 4, 5, 6 };
        var evens = intList.Where(x => x % 2 == 0);
    }

    {
        int[] intList = { 1, 2, 3, 4, 5, 6 };
        var odds = intList.Where(x => x % 2 == 1);
    }
}

Yes! That there are curly braces by themselves!

Why the hell would anyone want to do this? I find myself using them all the time when writing up samples where at http://www.formatexception.com. The reason is that I test all the sample code I post up here that I write. This has been a big help especially since it seems like a lot of the linq code here derives from msdn samples and, believe it or not, not all of them work.

What this means is that if I want to do a write-up on linq where I’m using the where method on IEnumerables (like above) I write the code surrounded by curly braces. Because each is it’s own scope, call it internal method scope if you will, there won’t be any name collisions. It’s like doing an if(true){} without the if(true), or like using the using keyword as in:

using(Stream ImageStream = new MemoryStream(File.ReadAllBytes(ImagePath)))
{
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = ImageStream;
    bi.EndInit();
    
    Image imageFromFile = new Image() { Source = bi };
    //dump image into wrap panel in form
    imagePanel.Children.Add(imageFromFile);
}

except you don’t have to have a class the implements IDisposable.

So, where would you use this? If you’re real paticular (i.e. anal) about the your variable names. For instance the following code is not valid:

{
    int [] intList = { 1, 2, 3, 4, 5, 6 };
    var evens = intList.Where(x => x % 2 == 0);
}


int[] intList = { 7, 8, 9, 10, 11, 12 };
var odds = intList.Where(x => x % 2 == 1);

The reason is that the set of curly braces define a child scope but the second intlist exists in the parent scope. Sibling scopes can have variables of the same name but not parent/child scopes. If I really wanted my intList to be named “intList” and assuming it would only be needed within the child scope I could put a second set of curly braces around the other code and be free to name my intList to, um, “intList”.

I have never really found a use for this in production code to be honest. I use it a lot when writing up samples for my posts, but not in production code. I suppose if I was adamant about my variable names I would use it more and I think a few of you out there are. I, however, have no problem simply naming my second int list to intListForOdds.

So that is the curly braces and their often unused purpose.

Later,
Brian

Leave a Reply

Your email address will not be published. Required fields are marked *

FormatException

928 East Plymouth Drive Asbury Park, NJ 07712