<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx</link><description>Title pretty much says it all but what good is a rule without any explanation. The main issue here is that at the core, using statements and lambda expressions both alter variable lifetimes. Unfortunately they alter the lifetime in different directions.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>a-foton &amp;raquo; Don&amp;#8217;t mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8738311</link><pubDate>Wed, 16 Jul 2008 15:34:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8738311</guid><dc:creator>a-foton &amp;raquo; Don&amp;#8217;t mix using statements and lambda expressions</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://blog.a-foton.ru/2008/07/dont-mix-using-statements-and-lambda-expressions/"&gt;http://blog.a-foton.ru/2008/07/dont-mix-using-statements-and-lambda-expressions/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>Don't Mix Lambda Expressions and Using Statements</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8738365</link><pubDate>Wed, 16 Jul 2008 15:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8738365</guid><dc:creator>Jesse Ezell Blog</dc:creator><description>&lt;p&gt;Lambda expressions are really cool, but they present some interesting problems because they aren't really&lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8740346</link><pubDate>Thu, 17 Jul 2008 00:21:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8740346</guid><dc:creator>Joe Enos</dc:creator><description>&lt;p&gt;Never would have thought of that one...Thanks.&lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8742514</link><pubDate>Thu, 17 Jul 2008 10:09:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8742514</guid><dc:creator>zproxy</dc:creator><description>&lt;p&gt;The program does what you say it should to. It looks like the programmer clearly does not understand when is what executed. By simply moving the using statement inside the delegate it will be fixed...&lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8744048</link><pubDate>Thu, 17 Jul 2008 17:42:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8744048</guid><dc:creator>Eddie Garmon</dc:creator><description>&lt;p&gt;You need to clarify your blanket rule. It is perfectly fine to use lambdas inside a using block. I believe what you meant to say is: Don’t take any lifetime dependencies upon the object being 'used', and return that object as a lifted variable in a lambda function. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Below is a contrived but perfectly valid example of a lambda inside a using.&lt;/p&gt;
&lt;p&gt;static void Example() {&lt;/p&gt;
&lt;p&gt; &amp;nbsp;int[] numbers = { 1, 2, 3, 4, 5 };&lt;/p&gt;
&lt;p&gt; &amp;nbsp;using (var obj = new MyDisosableObject()) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;numbers.ForEach(i =&amp;gt; Console.WriteLine(&amp;quot;{0} squared is {1}&amp;quot;, i, i * i));&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>Weekly Web Nuggets #21</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8759828</link><pubDate>Mon, 21 Jul 2008 03:25:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8759828</guid><dc:creator>Code Monkey Labs</dc:creator><description>&lt;p&gt;General Lessons That I Learned From Waegis : Keyvan Nayyeri shares the lessons he learned from launching Waegis , a spam filter for web sites. Don't Mix Using Statements And Lambda Expressions : Jared Parsons explains why you do not want to mix using&lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8764353</link><pubDate>Tue, 22 Jul 2008 19:13:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8764353</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;@Eddie,&lt;/p&gt;
&lt;p&gt;There are definately cases where this is valid and useful. &amp;nbsp;The issue I was refering to can be better summed up as &amp;quot;Don't use lamda expressions which do both of the following&amp;quot;&lt;/p&gt;
&lt;p&gt;1) Lambda extends the lifetime of a lifted variable beyond the current function&lt;/p&gt;
&lt;p&gt;2) Lifted variable is iteration or using variable. &lt;/p&gt;
&lt;p&gt;There are probably other cases in #2 that I'm missing. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;In general though I think it's better to have a more encompasing rule which disallows certain valid cases than a rule which is less encompassing and allows invalid cases. &amp;nbsp;I'm a bit paranoid though and I've been bitten by the iteration variable before so I tend to be picky about this. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8764543</link><pubDate>Tue, 22 Jul 2008 20:57:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8764543</guid><dc:creator>Eddie Garmon</dc:creator><description>&lt;p&gt;Ok, I do agree with the above. I had to go back and revalidate my Synchronized&amp;lt;T&amp;gt; as you made me think, (which is the point of this right?) as I do both of the above, but not at the same time.&lt;/p&gt;
</description></item><item><title>Syntactic Sugar at work</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8764601</link><pubDate>Tue, 22 Jul 2008 21:30:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8764601</guid><dc:creator>Ted</dc:creator><description>&lt;p&gt;Lambda expressions appear to be just syntactic sugar apart from querying a SQL server database.&lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8848354</link><pubDate>Mon, 11 Aug 2008 19:10:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8848354</guid><dc:creator>Orlando</dc:creator><description>&lt;p&gt;Jared I must say this is strange behavior. Shouldn't the using statement be able to override the scope extension of the lambda expression instead of allowing this inconsistent behavior? &lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8848418</link><pubDate>Mon, 11 Aug 2008 19:41:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8848418</guid><dc:creator>Jared Parsons</dc:creator><description>&lt;p&gt;@Orlando,&lt;/p&gt;
&lt;p&gt;I think you mean issue a compiler error because of the scope change. &amp;nbsp;If so in an ideal world this should be the case. &amp;nbsp;However it is not done at this time for a couple of reasons. &lt;/p&gt;
&lt;p&gt;The first is cost. &amp;nbsp;For the compiler to accurately spot this as an error it must determine that the lambda will actually live beyond the scope of the current function. &amp;nbsp;This is very expensive (if not impossible). &amp;nbsp;In this case the compiler would have to dig into the IL of every function the lambda was either passed to or called. &amp;nbsp;The analysis would include deep flow analysis to determine if the lambda was ever stored in a place that was eventually returned from the function or passed to a different thread or even to PInvoke. &amp;nbsp;This is very costly in both man hours to implement and compile time. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;The other is correctness. &amp;nbsp;Even though it's against design guidlines, there is nothing stopping you from using an object once it's been disposed. &amp;nbsp;You can in fact (please don't though) design an object to have a subset of it's properties/methods be valid after it's disposed. &amp;nbsp;In this case the code I laid out would still be correct so the compiler would be issuing a false warning. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8849484</link><pubDate>Tue, 12 Aug 2008 02:39:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8849484</guid><dc:creator>Kyle Sluder</dc:creator><description>&lt;p&gt;I think it makes sense to do this:&lt;/p&gt;
&lt;p&gt;func&amp;lt;IDisposable, string&amp;gt; f = x =&amp;gt; { using(x) { x.ToString() } };&lt;/p&gt;
&lt;p&gt;This certainly can't be a universally Bad Idea(TM), can it?&lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8852134</link><pubDate>Tue, 12 Aug 2008 19:17:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8852134</guid><dc:creator>James Hart</dc:creator><description>&lt;p&gt;LINQ expressions also introduce lambdas, less explicitly, and are particularly vulnerable to this sort of thing.&lt;/p&gt;
&lt;p&gt;IEnumerable&amp;lt;string&amp;gt; Names&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;get {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;using (var conn = new SqlConnection(ConnectionString))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;using (var cmd = new SqlCommand(&amp;quot;SELECT Name FROM People&amp;quot;, conn))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;conn.Open();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;return &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;from row in cmd.ExecuteReader().Cast&amp;lt;IDataRecord&amp;gt;() &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;select row[&amp;quot;Name&amp;quot;] as string;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;But then again, the same problem occurs if you just do this:&lt;/p&gt;
&lt;p&gt;SqlDataReader Names&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;get {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;using (var conn = new SqlConnection(ConnectionString))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;using (var cmd = new SqlCommand(&amp;quot;SELECT Name FROM People&amp;quot;, conn))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;conn.Open();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;return cmd.ExecuteReader();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;So it's really just another case of the danger of returning a deferred-execution object from within a using statement, which can be done with many things, such as a DataReader, an enumerator, a stream, or a lambda.&lt;/p&gt;
&lt;p&gt;Different bugs can be caused by passing out such objects from inside a lock {} block, too.&lt;/p&gt;
</description></item><item><title>re: Don't mix using statements and lambda expressions</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#8867567</link><pubDate>Thu, 14 Aug 2008 21:26:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8867567</guid><dc:creator>Tanveer Badar</dc:creator><description>&lt;p&gt;I think it is the C# version of this C++ code&lt;/p&gt;
&lt;p&gt;int* dummy( )&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; int d = 0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; return &amp;amp;d;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>Weekly Web Nuggets #21</title><link>http://blogs.msdn.com/jaredpar/archive/2008/07/16/don-t-mix-using-statements-and-lambda-expressions.aspx#9440905</link><pubDate>Mon, 23 Feb 2009 06:47:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9440905</guid><dc:creator>Code Monkey Labs</dc:creator><description>&lt;p&gt;General Lessons That I Learned From Waegis : Keyvan Nayyeri shares the lessons he learned from launching Waegis , a spam filter for web sites. Don't Mix Using Statements And Lambda Expressions : Jared Parsons explains why you do not want to mix using&lt;/p&gt;
</description></item></channel></rss>