1

I have this method and get the above error on the line words.Add(rows); can anyone help? Thanks - Ben

private static IEnumerable<string> LoadWords(String filePath)
    {

        List<String> words = new List<String>();

        try
        {
            foreach (String line in File.ReadAllLines(filePath))
            {
                string[] rows = line.Split(',');

                words.Add(rows);
            }

        }
        catch (Exception e)
        {
            System.Windows.MessageBox.Show(e.Message);
        }

            return words;
    }
Ben
  • 1,113
  • 5
  • 15
  • 25
  • For your own information, [here](http://stackoverflow.com/questions/3891157/an-i-prevent-a-specific-type-using-generic-restrictions/3891439#3891439) Eric Lippert explains why it makes sense for `Add` and `AddRange` are separate functions. [Here](http://stackoverflow.com/questions/3891157/an-i-prevent-a-specific-type-using-generic-restrictions/3891584#3891584) LBushkin goes into a bit more detail, looking at the technical limitations that make this choice necessary. Without those technical limitations, having two separate functions would still be a good idea, though. – Brian Mar 18 '11 at 17:38

8 Answers8

6

Instead of

words.Add(rows);

use this :

words.AddRange(rows);

rows is a string array containing multiple strings, so you have to add them with AddRange().

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
4

Change it to this

words.AddRange(rows);

You issue is that you are adding an array of items, not a single element.

You use AddRange() when adding a collection that implements System.Collections.Generic.IEnumerable<T>

See documentation here http://msdn.microsoft.com/en-us/library/z883w3dc.aspx

Matthew Cox
  • 13,566
  • 9
  • 54
  • 72
1

You are trying to add a string array to a list that takes a string.

Try words.AddRange(rows);

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
1

u r trying to add string of array in a list of array

private static IEnumerable<string> LoadWords(String filePath)
    {

        List<String> words = new List<String>();

        try
        {
            foreach (String line in File.ReadAllLines(filePath))
            {
                string[] rows = line.Split(',');

                foreach(string str in rows)
                       words.Add(str);
            }

        }
        catch (Exception e)
        {
            System.Windows.MessageBox.Show(e.Message);
        }

            return words;
    }
Avi
  • 251
  • 1
  • 4
  • 14
0

You are using the wrong method. You want the AddRange method.

words.AddRange(rows);
Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
0

Have a try of this:

words.AddRange(rows);
zs2020
  • 53,766
  • 29
  • 154
  • 219
0

.Add will take another string, not an array of strings.

Try .AddRange instead.

iggymoran
  • 4,059
  • 2
  • 21
  • 26
0
private static IEnumerable<string> LoadWords(String filePath)
{

    List<String> words = new List<String>();

    try
    {
        foreach (String line in File.ReadAllLines(filePath))
        {
            string[] rows = line.Split(',');

            foreach (String word in rows)
            {
                words.Add(word);
            }
        }

    }
    catch (Exception e)
    {
        System.Windows.MessageBox.Show(e.Message);
    }

        return words;
}
  • +0: This will work, but just calling `AddRange` is cleaner than a `foreach`. – Brian Mar 18 '11 at 17:39
  • I agree, but if the OP needs to see a way to exclude certain words, this would let him see where to add a piece of code to test before inserting. –  Mar 18 '11 at 19:15