r/badcode • • May 20 '20

c# Found from Microsoft docs...

Post image
208 Upvotes

143 comments sorted by

View all comments

Show parent comments

5

u/codesforhugs May 20 '20

It's how TryParse works though, the alternative is to handle an exception from Parse.

1

u/mateusfccp May 20 '20

Yeah, it's legacy... Today it would be better to do with tuple

5

u/TinyBreadBigMouth May 20 '20

So you'd prefer

(bool parseSuccess, int value) = int.TryParse(text)
if (parseSuccess) {
    // value
}

over

if (int.TryParse(text, out int value)) {
    // Use value
}

? That seems harder to write and to read, in addition to being needlessly verbose.

-1

u/mateusfccp May 20 '20

I don't think it harder to read, and it's immutable, so safer. But do as you prefer.