C # index out of range String Array and List <string>

After trying several methods for checking errors, I came to the conclusion that I need help in solving this problem.

As I do not catch this error "index out of range". What can I do to avoid this problem in the future for good practice?

    public void loadFromFile()
    {
        OpenFileDialog oFile = new OpenFileDialog();
        oFile.Title = "Open text file";
        oFile.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        oFile.FilterIndex = 1;
        oFile.InitialDirectory = Application.StartupPath;
        oFile.AddExtension = true;
        oFile.CheckFileExists = true;
        oFile.CheckPathExists = true;

        // Open and clean Duplicates
        String[] lines;
        List<string> temp = new List<string>();
        List<string> newlist = new List<string>();

        if(oFile.ShowDialog() == DialogResult.OK)
        {
            // Dummy file has 6 lines of text. Filename:DuplicatFile.txt
            // 3 duplicate lines and 3 not.
            lines = File.ReadAllLines(oFile.FileName, System.Text.Encoding.UTF8);

            // Copy array to temporary array
            for (int index=0; index < lines.Length; index++)
            {
                // System.ArgumentOutOfRangeException was unhandled
                // Index was out of range. Must be non-negative and less than the size of the collection.
                if (lines[index].Length >= 0)
                {
                    temp[index] = lines[index];
                }
            }
            // Check for duplicates. If duplicate ignore if non-duplicate add to list.
            foreach (string line in temp)
            {
                if (!newlist.Contains(line))
                {
                    newlist.Add(line);
                }
            }
            // Clear listbox and add new list to listbox.
            lstBox.Items.Clear();
            foreach (string strNewLine in newlist)
            {
                lstBox.Items.Add(strNewLine);
            }
        }
    }
+5
source share
4 answers
List<string> temp = new List<string>();
...
temp[index] = lines[index];

temp starts at size 0. Any index is out of range.

You can fix this by using temp.Addto make a dynamic dynamic list:

temp.Add(lines[index]);
+8
source

Mud ArgumentOutOfRangeException. if Linq :

lines = File.ReadAllLines(oFile.FileName, System.Text.Encoding.UTF8);    
lstBox.Items.AddRange(lines.Distinct().ToArray());
+2

, "" ​​ - , "temp" ... "temp", . 0!

.Add:

temp.Add(lines[index])

... , , , .

+1

, temp. (temp ). temp.Add(value).

temp temp = newlist.ToList().

LINQ:

lstBox.Items.Clear();
foreach (var line in lines.Distinct())
    lstBox.Items.Add(line);

instead of all this code:

// Copy array to temporary array
for (int index=0; index < lines.Length; index++)
{
    // System.ArgumentOutOfRangeException was unhandled
    // Index was out of range. Must be non-negative and less than the size of the collection.
    if (lines[index].Length >= 0)
    {
        temp[index] = lines[index];
    }
 }
 // Check for duplicates. If duplicate ignore if non-duplicate add to list.
 foreach (string line in temp)
 {
   if (!newlist.Contains(line))
   {
       newlist.Add(line);
   }
 }
 lstBox.Items.Clear();
 foreach (string strNewLine in newlist)
 {
    lstBox.Items.Add(strNewLine);
 }

to simple:

+1
source

All Articles