When accessing an array of objects, I think it is clear to define a variable like 'obj' in the for loops, as follows:
var objects = new object[10000];
//......assign values to objects
//When visiting objects, I think it is clear to define a variable 'obj' in the for loops
for (int i = 0; i < objects.Length; i++)
{
var obj = objects[i];
//......visit obj which will be used for many times, so define the obj to simplify the codes
}
But I am concerned with its efficiency. I worry that repeated 'obj' will be created.
Should I define the variable in front of the for loops as follow? As well as defining a variable for objects.Length.
int len = objects.Length;
object obj2;
for (int i = 0; i < len; i++)
{
obj2 = objects[i];
//visit obj
//......
}
Or compiler in Visual Studio will optimize for it?