In c# can I do something like this in a shorthand?
bool validName = true;
if (validName)
{
name = "Daniel";
surname = "Smith";
}
else
{
MessageBox.Show("Invalid name");
}
I was just wondering if something similar to this would work, but in this exact scenario, I know you can assign values if I did name = validName ? "Daniel" : "Not valid", but I was just wondering if i can do the below?
validName ?
{
name = "Daniel";
surname = "Smith";
}
:
{
MessageBox.Show("Invalid name");
}
Abusing lambda syntax and type inference:
I know, not really an answer. If statement is way better: obviously this syntax is less readable and more importantly it has different runtime behaviour and could lead to unintended side effects because of potential closures created by the lambda expressions.
Syntax is a bit cryptic too. It first creates two
Actionobjects then?:operator chooses between them and at last the chosen result is executed:I put it together in one statement by executing the resulting action in place. To make it even briefer I cast the first lambda expression into an
Action(instead of creating anew Action()explicitly) and taking advantage of type inference I left out the second cast.