Click here to Skip to main content
Click here to Skip to main content

Breaking Changes in Argument List Evaluation in C# 5.0

By , 30 Jul 2012
 

The C# Language Specification states on §7.5.1.2 that “(…) the expressions or variable references of an argument list are evaluated in order, from left to right (…)”.free hit counters

So, when this code is compiled with the C# 4.0 compiler:

static void M(
    int x = 10,
    int y = 20,
    int z = 30)
{
    Console.WriteLine(
        "x={0}, y={1}, z={2}", x, y, z);
}

static void Main(string[] args)
{
    int a = 0;

    M(++a, z: ++a);
}

and run, this unexpected output is obtained:

x=2, y=20, z=1

In fact, fixing this compiler flaw was the cause of one of the few breaking changes introduced in C# 5.0.

Using the 5.0 compiler, the expected result is obtained:

x=1, y=20, z=2

To avoid this type of surprises, expression evaluation should be avoided in argument lists.

With this code:

int a = 0;

int i = ++a;
int j = ++a;

M(i, z: j);

the same result is obtained for both C# 4.0 and C# 5.0:

x=1, y=20, z=2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Paulo Morgado
Software Developer (Senior) Paulo Morgado
Portugal Portugal
Member

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4membereferreyra30 Jul '12 - 3:05 
GeneralExcellent ARTICLEmemberSSerjo26 Jul '12 - 8:04 
GeneralRe: Excellent ARTICLEmemberPaulo Morgado26 Jul '12 - 8:24 
GeneralMy vote of 5memberjohannesnestler26 Jul '12 - 6:05 
GeneralRe: My vote of 5memberPaulo Morgado26 Jul '12 - 8:23 
QuestionMessage Automatically Removedmemberfkjgjhjhjnj26 Jul '12 - 3:58 
QuestionNot an article...mvpDave Kreskowiak26 Jul '12 - 3:38 
AnswerRe: Not an article...memberPaulo Morgado26 Jul '12 - 4:04 
GeneralNot an articlemvpRichard MacCutchan25 Jul '12 - 23:09 
GeneralRe: Not an articlememberPaulo Morgado26 Jul '12 - 0:34 
GeneralRe: Not an articlemvpRichard MacCutchan26 Jul '12 - 1:17 
GeneralRe: Not an articlememberPaulo Morgado26 Jul '12 - 3:46 
GeneralRe: Not an articlemvpRichard MacCutchan26 Jul '12 - 5:18 
GeneralRe: Not an articlememberPaulo Morgado26 Jul '12 - 5:27 
GeneralRe: Not an articlemvpRichard MacCutchan26 Jul '12 - 5:56 
GeneralRe: Not an articlememberPaulo Morgado26 Jul '12 - 6:14 
GeneralRe: Not an articlememberMatt T Heffron26 Jul '12 - 8:29 
GeneralRe: Not an articlememberPaulo Morgado26 Jul '12 - 8:50 
GeneralRequires more explanation and claritymemberPIEBALDconsult25 Jul '12 - 7:10 
GeneralRe: Requires more explanation and claritymemberPaulo Morgado25 Jul '12 - 13:41 
GeneralRe: Requires more explanation and claritymemberPIEBALDconsult26 Jul '12 - 3:21 
GeneralRe: Requires more explanation and claritymemberPaulo Morgado26 Jul '12 - 3:34 
GeneralRe: Requires more explanation and claritymemberPIEBALDconsult26 Jul '12 - 3:57 
GeneralRe: Requires more explanation and claritymemberPaulo Morgado26 Jul '12 - 4:06 
GeneralRe: Requires more explanation and claritymemberPIEBALDconsult26 Jul '12 - 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 30 Jul 2012
Article Copyright 2012 by Paulo Morgado
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid