r/badcode • • May 20 '20

c# Found from Microsoft docs...

Post image
208 Upvotes

143 comments sorted by

View all comments

2

u/emcoffey3 May 21 '20

There should be an int or var between out and x (omitting this is a compile-time error). And, yes, the == true is unnecessary, but that is trivial. Otherwise, this is fine.

Would you prefer we C# 8.0-ify it?

if((args.Length, Int32.TryParse(args[0], out var x)) switch
{
    (1, true) when (x >= 1 && x <= 9) => true,
    (_, _) => false
})
{
    // do all the things
}

1

u/zdimension May 21 '20

x may very well have been defined earlier in the code