else or return?

后端 未结 19 814
故里飘歌
故里飘歌 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 13:58

    Personally I always like to return ASAP so I would go for something like:

    if (result)
    {
        // do something
        return;
    }
    
    // do something if not result
    

    With regards to performance, I doubt either have any advantages over eachother it really comes down to readability and personal taste. I assume .NET would optimize your first code block to something like the above.

提交回复
热议问题