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;
    }
1
  • For your own information, here Eric Lippert explains why it makes sense for Add and AddRange are separate functions. Here 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. Commented Mar 18, 2011 at 17:38

8 Answers 8

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().

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

Try words.AddRange(rows);

Comments

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;
    }

Comments

0

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

words.AddRange(rows);

Comments

0

Have a try of this:

words.AddRange(rows);

Comments

0

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

Try .AddRange instead.

Comments

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;
}

2 Comments

+0: This will work, but just calling AddRange is cleaner than a foreach.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.