Reference type variable recycling - is a new reference variable created every loop in a loop if declared therein?

后端 未结 3 1980
梦谈多话
梦谈多话 2020-11-28 07:54

Is the following:

MyObject myVariable;
for(int i = 0; i < objects.Length, i++){
  myVariable = objects[i];
  // do stuff...
}

more effic

3条回答
  •  被撕碎了的回忆
    2020-11-28 08:41

    short answer, yes.

    long answer, yes it is faster, but hardly noticeable unless repeated many times. :-)

    I am not sure if the compiler will optimize it or not, I doubt it though, and if it does, good for it, you should still write it as if it doesn't, make it a habbit.

提交回复
热议问题