Initializing a 'var' to null

前端 未结 2 1987
春和景丽
春和景丽 2020-12-18 20:13

Is there any difference in runtime performance between the following variable initializations?

var    x = null as object;
var    x = (object) null;
object x          


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-18 20:32

    First of all: No, I believe these three calls are essentially equivalent.

    Secondly: Even if there was any difference between them, it would surely be so minuscule that it would be completely irrelevant in an application.

    This is such a tiny piece of any program, that focusing on optimization here and in similar situations, will often be a waste of time, and might in some cases make your code more complicated for no good reason.

    There is a longer interesting discussion about this on the programmers.stackexchange site.

提交回复
热议问题