Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C# 4.0

Breaking Changes in Argument List Evaluation in C# 5.0

Rate me:
Please Sign up or sign in to vote.
4.86/5 (13 votes)
30 Jul 2012CPOL 35.4K   11   25
In C# 4.0 there was an error in the C# compiler in the order of the evaluation of the arguments in argument lists.

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:

C#
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:

C#
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:

C#
x=1, y=20, z=2

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

With this code:

C#
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:

C#
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)


Written By
Software Developer (Senior) Paulo Morgado
Portugal Portugal

Comments and Discussions

 
GeneralMy vote of 4 Pin
eferreyra30-Jul-12 3:05
eferreyra30-Jul-12 3:05 
GeneralExcellent ARTICLE Pin
Tikoko26-Jul-12 8:04
Tikoko26-Jul-12 8:04 
GeneralRe: Excellent ARTICLE Pin
Paulo Morgado26-Jul-12 8:24
professionalPaulo Morgado26-Jul-12 8:24 
GeneralMy vote of 5 Pin
johannesnestler26-Jul-12 6:05
johannesnestler26-Jul-12 6:05 
Good to know, thank you - maybe not an "article" though... but you got my 5
GeneralRe: My vote of 5 Pin
Paulo Morgado26-Jul-12 8:23
professionalPaulo Morgado26-Jul-12 8:23 
QuestionNot an article... Pin
Dave Kreskowiak26-Jul-12 3:38
mveDave Kreskowiak26-Jul-12 3:38 
AnswerRe: Not an article... Pin
Paulo Morgado26-Jul-12 4:04
professionalPaulo Morgado26-Jul-12 4:04 
GeneralNot an article Pin
Richard MacCutchan25-Jul-12 23:09
mveRichard MacCutchan25-Jul-12 23:09 
GeneralRe: Not an article Pin
Paulo Morgado26-Jul-12 0:34
professionalPaulo Morgado26-Jul-12 0:34 
GeneralRe: Not an article Pin
Richard MacCutchan26-Jul-12 1:17
mveRichard MacCutchan26-Jul-12 1:17 
GeneralRe: Not an article Pin
Paulo Morgado26-Jul-12 3:46
professionalPaulo Morgado26-Jul-12 3:46 
GeneralRe: Not an article Pin
Richard MacCutchan26-Jul-12 5:18
mveRichard MacCutchan26-Jul-12 5:18 
GeneralRe: Not an article Pin
Paulo Morgado26-Jul-12 5:27
professionalPaulo Morgado26-Jul-12 5:27 
GeneralRe: Not an article Pin
Richard MacCutchan26-Jul-12 5:56
mveRichard MacCutchan26-Jul-12 5:56 
GeneralRe: Not an article Pin
Paulo Morgado26-Jul-12 6:14
professionalPaulo Morgado26-Jul-12 6:14 
GeneralRe: Not an article Pin
Matt T Heffron26-Jul-12 8:29
professionalMatt T Heffron26-Jul-12 8:29 
GeneralRe: Not an article Pin
Paulo Morgado26-Jul-12 8:50
professionalPaulo Morgado26-Jul-12 8:50 
GeneralRequires more explanation and clarity Pin
PIEBALDconsult25-Jul-12 7:10
mvePIEBALDconsult25-Jul-12 7:10 
GeneralRe: Requires more explanation and clarity Pin
Paulo Morgado25-Jul-12 13:41
professionalPaulo Morgado25-Jul-12 13:41 
GeneralRe: Requires more explanation and clarity Pin
PIEBALDconsult26-Jul-12 3:21
mvePIEBALDconsult26-Jul-12 3:21 
GeneralRe: Requires more explanation and clarity Pin
Paulo Morgado26-Jul-12 3:34
professionalPaulo Morgado26-Jul-12 3:34 
GeneralRe: Requires more explanation and clarity Pin
PIEBALDconsult26-Jul-12 3:57
mvePIEBALDconsult26-Jul-12 3:57 
GeneralRe: Requires more explanation and clarity Pin
Paulo Morgado26-Jul-12 4:06
professionalPaulo Morgado26-Jul-12 4:06 
GeneralRe: Requires more explanation and clarity Pin
PIEBALDconsult26-Jul-12 5:23
mvePIEBALDconsult26-Jul-12 5:23 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.