using System;
namespace Optimization
{
class Program
{
static void Main(string[] args)
{
Int32[] arr ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,11 };
for (Int32 i = 0; i<=arr.Length-1;i++) {
Console.WriteLine(arr[i]);
}
Console.ReadLine();
}
}
}
This is the code that I wrote to print a simple Int32 array. Below is the IL generated by this code snippet.
Compiler stores the array length in line IL_0000. The you can see there is no call to Array.Length property [internally it will call the method get_Length()] which I have used in For loop. Only calls that C# compiler added were to Initialize array, WriteLine() and ReadLine(). C# compiler have optimized our code internally so that there is no need to call Length with every loop iteration. It uses the length that is stored in the beginning to provide a performance boost. So, if you are doing such thing in your code, don't complicate things by declaring a variable just before the for loop just to store array length and use that variable in loop condition. You will be doing that to improve your application's performance but doing such heroics is not needed in this scenario.
I have just started to read about ASP.net internals and currently I am dating HTTPSys driver :). I hope to get this done by November 2nd week.
Currently listening To: SHAGGY Feat. AKON - What's Love
...nJoy CoDiNg...