Are these two statements identical in what they return?
public object Switcher(bool flag)
{
switch (flag) {
case true:
return 1;
case false:
return 10.5;
}
}
public object Switcher(bool flag)
{
return flag switch {
true => 1,
false => 10.5
};
}
No.
Test | Result – Switch Statement | Result – Switch Expression |
---|---|---|
Assert.Equal(1, x.Switcher(true)); | Pass | Fail – Error Message:Assert.Equal() Failure |
Assert.Equal(10.5, x. Switcher (false)); | Pass | Pass |
Scratching your head trying to find the difference? The logic in both looks identical. Arguably, it is.
Continue reading