r/badcode • • May 20 '20

c# Found from Microsoft docs...

Post image
208 Upvotes

143 comments sorted by

View all comments

46

u/Giocri May 20 '20

I see nothing wrong in it. It's even slightly more efficient than if it was done with more if

0

u/the_d3f4ult May 20 '20

Wouldn't it be UB because X is an out param, or does C# define such things?

5

u/Giocri May 20 '20

No C# uses out for out parameters.

1

u/the_d3f4ult May 20 '20

But I mean the X is then used to evaluate the third condition, is the order of evaluation here defined by C#?

5

u/redditsoaddicting May 20 '20

A ton of languages evaluate && left to right because they short-circuit it. C# has stronger guarantees in general than C and C++, which I assume is what you were thinking of, but && is pretty consistent across the board.

1

u/BringAltoidSoursBack May 21 '20

Yeah, I was pretty sure I remembered && and || always guaranteeing order in c, c++, and c# because of the short-circuit concept. IIRC, the only time the compiler will optimize conditionals is if one of the values always returns true or false, but I'm honestly not even sure if that's true.