<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FormatException &#187; often unused operators</title>
	<atom:link href="http://www.formatexception.com/tag/often-unused-operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.formatexception.com</link>
	<description>Ramblings on developing in the Windows World</description>
	<lastBuildDate>Mon, 24 May 2010 15:43:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Often Unused Operators: Curly Braces {}</title>
		<link>http://www.formatexception.com/2009/04/often-unused-operators-curly-braces/</link>
		<comments>http://www.formatexception.com/2009/04/often-unused-operators-curly-braces/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 15:15:12 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[often unused operators]]></category>

		<guid isPermaLink="false">http://www.formatexception.com/?p=209</guid>
		<description><![CDATA[Okay, I know you think I&#8217;ve seriously hit the crack pipe here.  Curly Braces, as I&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, I know you think I&#8217;ve seriously hit the crack pipe here.  Curly Braces, as I&#8217;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.</p>
<p>So how could I dare to say the {} is an often unused operator?  Well, I&#8217;m specifically talking about a use of the operators in this case, not the operators themselves.</p>
<p>Did you know you can use curly braces by themselves?  Take a look at this:</p>
<pre name="code" class="csharp">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);
    }
}</pre>
<p>Yes! That there are curly braces by themselves!</p>
<p>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.</p>
<p>What this means is that if I want to do a write-up on linq where I&#8217;m using the where method on IEnumerables (like above) I write the code surrounded by curly braces.  Because each is it&#8217;s own scope, call it internal method scope if you will, there won&#8217;t be any name collisions.  It&#8217;s like doing an if(true){} without the if(true), or like using the using keyword as in:  </p>
<pre name="code" class="csharp">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);
}</pre>
<p>except you don&#8217;t have to have a class the implements IDisposable.</p>
<p>So, where would you use this?  If you&#8217;re real paticular (i.e. anal) about the your variable names.  For instace the following code is <span style="color: #ff0000;">not valid</span>:</p>
<pre name="code" class="csharp">{
    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);</pre>
<p>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 &#8220;intList&#8221; 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, &#8220;intList&#8221;.</p>
<p>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.</p>
<p>So that is the curly braces and their often unused purpose.</p>
<p>Later,<br />
Brian</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.formatexception.com%2F2009%2F04%2Foften-unused-operators-curly-braces%2F&amp;title=Often%20Unused%20Operators%3A%20Curly%20Braces%20%7B%7D&amp;bodytext=Okay%2C%20I%20know%20you%20think%20I%27ve%20seriously%20hit%20the%20crack%20pipe%20here.%20%20Curly%20Braces%2C%20as%20I%27m%20sure%20we%20all%20know%2C%20are%20fundamental%20to%20programming%20in%20most%20languages.%20%20This%20is%20true%20no%20more%20so%20then%20here%20in%20C%23.%20%20We%20use%20them%20for%20if%2C%20for%2C%20using%2C%20do%2C%20while%2C%20namespace%20s" title="Digg"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.formatexception.com%2F2009%2F04%2Foften-unused-operators-curly-braces%2F&amp;t=Often%20Unused%20Operators%3A%20Curly%20Braces%20%7B%7D" title="Facebook"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fwww.formatexception.com%2F2009%2F04%2Foften-unused-operators-curly-braces%2F&amp;title=Often%20Unused%20Operators%3A%20Curly%20Braces%20%7B%7D" title="DotNetKicks"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/dotnetkicks.png" title="DotNetKicks" alt="DotNetKicks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.formatexception.com%2F2009%2F04%2Foften-unused-operators-curly-braces%2F&amp;title=Often%20Unused%20Operators%3A%20Curly%20Braces%20%7B%7D&amp;annotation=Okay%2C%20I%20know%20you%20think%20I%27ve%20seriously%20hit%20the%20crack%20pipe%20here.%20%20Curly%20Braces%2C%20as%20I%27m%20sure%20we%20all%20know%2C%20are%20fundamental%20to%20programming%20in%20most%20languages.%20%20This%20is%20true%20no%20more%20so%20then%20here%20in%20C%23.%20%20We%20use%20them%20for%20if%2C%20for%2C%20using%2C%20do%2C%20while%2C%20namespace%20s" title="Google Bookmarks"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.formatexception.com%2F2009%2F04%2Foften-unused-operators-curly-braces%2F&amp;title=Often%20Unused%20Operators%3A%20Curly%20Braces%20%7B%7D" title="Live"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.formatexception.com%2F2009%2F04%2Foften-unused-operators-curly-braces%2F&amp;t=Often%20Unused%20Operators%3A%20Curly%20Braces%20%7B%7D" title="HackerNews"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.formatexception.com/2009/04/often-unused-operators-curly-braces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
