else or return?

后端 未结 19 903
故里飘歌
故里飘歌 2021-01-01 13:27

Which one out of following two is best wrt to performance and standard practice. How does .NET internally handles these two code snippets?

Code1

If(r         


        
19条回答
  •  情深已故
    2021-01-01 14:07

    If you remove the braces around the 2nd version's remaining code, that's exactly what I use. I prefer to make the early validation part of a function obvious, and then I can get down to business.

    That being said, it's a matter of opinion. As long as you're consistent about it, pick one and stick with it.

    edit: About the performance, the emitted IL is exactly the same. Choose one or the other for the style, there is no penalty to either.

提交回复
热议问题