else or return?

后端 未结 19 581
故里飘歌
故里飘歌 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:11

    The performance difference, if any, is negligible in any normal case.

    One standard practice (among others) is to try to keep a single exit point from a method, so that talks in favour of the first alternative.

    The actual implementation for the return in the middle is most likely to make a jump to the end of the method, where the code to wrap up the stack frame for the method is, so it's likely that the final executable code is identical for the two codes.

    0 讨论(0)
提交回复
热议问题