OdeToCode IC Logo

C# 6.0 Features Part 3: Declaration Expressions

Monday, September 15, 2014

Update: this feature was dropped from C# 6.0. 

In C# we’ve always had declaration statements for declaring variables, and expression statements to produce values.

In C# 6 we can mix a declarations with an expression to declare a new variable and produce a value.

The canonical example is the int.TryParse method, which requires an out parameter. In previous versions of C# the parameter would be declared in a distinct statement before the invocation of TryParse.

int result;
if(int.TryParse(input, out result))
    return result;
return 0; // note: result still in scope

With C# 6.0, the same result is achievable with a declaration expression. The declaration statement also limits the scope of the variable passed as an out parameter.

if (int.TryParse(input, out var result))
    return result;
return 0; // result is out of scope

We could also transform the above code into a one-liner. . .

return int.TryParse(input, out var result) ? result : 0;

. . . and then package it into an extension method for string.

public static int ParseIntSafe(this string input, int @default = 0)
    return int.TryParse(input, out var result) ? result : @default;

With declaration expressions we can also declare variables inside if statements . . .

if ((var address = user.HomeAddress) != null)
    // work with address ...

    return address.City;

. . . and the various looping statements.

int result = 0;
foreach (var n in var odd = numbers.Where(n => n % 2 == 1).ToList())
    result += n + odd.Count();
return result;

Declaration are a small addition to the C# language, but will come in useful in a few scenarios.

Tuesday, September 16, 2014
Hi, is there an alternate way for this code with this new syntactic sugar: { var x = new (); try{DoSome1()} ctach{DoSome2()} finally{x.Dispose()} }
Gravatar Chris Surfleet Tuesday, September 16, 2014
This is one of those features that used correctly will be really useful, but used too much could make code almost unreadable!
Stas Tuesday, September 16, 2014
So what will be the scope? for int.TryParse I guess it will be outside the if statement. i.e. it will have the int.TryParse scope. But for declaring if ((var address = user.HomeAddress) != null) {...} it should be only within the if block, right?
Gravatar Scott Tuesday, September 16, 2014
@Stas: From reading through the latest round of C# design notes, I believe address stays within the if block.
Gravatar Paul Tuesday, September 16, 2014
I swear I have done variable declaration in if statements in .NET 4.0.
Wednesday, September 17, 2014
Paul, you're wrong, you get compilation error of invalid expression term. BTW, I think this "scopofobia" is too much. if you're modular enough it is not such a big problem. it is nice to have everything in its place but the price is confusing hard to debug code. kind of like using linq for complicated stuff.
Gravatar oldbastard Wednesday, September 17, 2014
"and then package it into an extension method for string." If you package it, who cares if the internal code is a one liner or a two liner? You should probably do this anyway. "With declaration expressions we can also declare variables inside if statements . . ." Because that's a great idea. foreach (var n in var odd = numbers.Where(n => n % 2 == 1).ToList()) What an awful line of code. Looks like these are great additions to c#...
Gravatar Eric Lippert Wednesday, September 17, 2014
Another nice extension method is "static int? ParseInt(this string s) { return int.TryParse(s, out int result) ? new int?(result) : new int?(); }"
Tuesday, September 23, 2014
scott, do you plan to post on the new "?." operator. I'm interested especially in its design aspects.
Gravatar scott Tuesday, September 23, 2014
Comments are closed.