I wonder why this syntax is not allowed in c#?
var email = (User user = User.Current) != null ? user.Email : "[email protected]";
When this is allowed:
User user;
var email = (user = User.Current) != null ? user.Email : "[email protected]";
Why does the variable have to be defined already, and cannot be defined in the statement?
This would help to make lines shorter, for example:
var email = User.Current.Very.Complex.Path != null ? User.Current.Very.Complex.Path.Email : "[email protected]";
could be
var email = (var user = User.Current.Very.Complex.Path) != null ? user.Email : "[email protected]";
a
can be used inusing(var a = GetVar()){ /*use a*/}
. Ie: variables declared in one line if's can be used in the rest of the statement. – QuiteriUser user; var email = (user = User.Current.Very.Complex.Path) != null ? user.Email : "[email protected]";
– Sausagevar email = (User.Current ?? new User { Email = "[email protected]" }).Email;
– QuiteriDeclaration Expressions
almost made it into c# 6.0 and will make it into a future release sooner or later. – Toucan