C# Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace CompareRegistryFiles { class Program { static void Main(string[] args) { string sDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string sFilePath = sDesktopPath + "\\" + "3.txt"; using (StreamReader oStreamReader = new StreamReader(sFilePath)) { string line; string[] arr = new String[3]; int x = 0; while ((line = oStreamReader.ReadLine()) != null) { // 3.txt // a // b // [space] // d Console.WriteLine(x + " " + line); // if string[] arr = new String[3] // error: Index was outside the bounds of the array. arr[x] = line; x++; } Console.ReadLine(); } } } }
Please see the comments above.
If
if string[] arr = new String[4],
no error.
But there is only 4 lines and the arr index x starts from 0. So the size of arr should be no more than 3.
Could anyone explain to me why the error occurs?
Thanks.


Reply With Quote
< Please rate my post if this helped you out. Any kind of thanks is gladly appreciated > 

