<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>Search results for 'app:weblogs' matching tag 'C#5.0'</title><link>http://msmvps.com/search/SearchResults.aspx?q=app:weblogs&amp;tag=C%235.0&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tag 'C#5.0'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Breaking Changes In Argument List Evaluation In C# 5.0</title><link>http://msmvps.com/blogs/paulomorgado/archive/2012/07/18/breaking-changes-in-argument-list-evaluation-in-c-5-0.aspx</link><pubDate>Wed, 18 Jul 2012 05:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1812975</guid><dc:creator>paulo</dc:creator><description>&lt;p&gt;The &lt;a title="C# Language Specification" href="http://msdn.microsoft.com/library/ms228593.aspx" target="_blank"&gt;C# Language Specification&lt;/a&gt; states on §7.5.1.2 that “(…) the expressions or variable references of an argument list are evaluated in order, from left to right (…)”.&lt;/p&gt;  &lt;p&gt;So, when this code is compiled with the &lt;a title="Microsoft Visual C#" href="http://csharp.net/" target="_blank"&gt;C#&lt;/a&gt; 4.0 compiler:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;static void &lt;/span&gt;M(
    &lt;span style="color:blue;"&gt;int &lt;/span&gt;x = 10,
    &lt;span style="color:blue;"&gt;int &lt;/span&gt;y = 20,
    &lt;span style="color:blue;"&gt;int &lt;/span&gt;z = 30)
{
    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(
        &lt;span style="color:#a31515;"&gt;&amp;quot;x={0}, y={1}, z={2}&amp;quot;&lt;/span&gt;, x, y, z);
}

&lt;span style="color:blue;"&gt;static void &lt;/span&gt;Main(&lt;span style="color:blue;"&gt;string&lt;/span&gt;[] args)
{
    &lt;span style="color:blue;"&gt;int &lt;/span&gt;a = 0;

    M(++a, z: ++a);
}&lt;/pre&gt;

&lt;p align="justify"&gt;and run, this unexpected output is obtained:&lt;/p&gt;

&lt;pre class="code"&gt;x=2, y=20, z=1&lt;/pre&gt;

&lt;p align="justify"&gt;In fact, fixing this compiler flaw was the cause of one of the few &lt;a title="Visual C# Breaking Changes in Visual Studio 2012" href="http://msdn.microsoft.com/library/hh678682(v=vs.110).aspx" target="_blank"&gt;breaking changes introduced in C# 5.0&lt;/a&gt;.&lt;/p&gt;

&lt;p align="justify"&gt;Using the 5.0 compiler, the expected result is obtained:&lt;/p&gt;

&lt;pre class="code"&gt;x=1, y=20, z=2&lt;/pre&gt;

&lt;p align="justify"&gt;To avoid this type of surprises, expression evaluation should be avoided in argument lists.&lt;/p&gt;

&lt;p align="justify"&gt;With this code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;int &lt;/span&gt;a = 0;

&lt;span style="color:blue;"&gt;int &lt;/span&gt;i = ++a;
&lt;span style="color:blue;"&gt;int &lt;/span&gt;j = ++a;

M(i, z: j);&lt;/pre&gt;

&lt;p align="justify"&gt;the same result is obtained for both C# 4.0 and C# 5.0:&lt;/p&gt;

&lt;pre class="code"&gt;x=1, y=20, z=2&lt;/pre&gt;</description></item></channel></rss>