-2

I do not understand, what is the problem?

my code:

public List<string> files = new List<string>();
string bb="C:\\cpqsystem";
files.AddRange(bb);

error description:

error CS1502: The best overloaded method match for 'System.Collections.Generic.List<string>.AddRange(System.Collections.Generic.IEnumerable<string>)' has some invalid arguments
error CS1503: Argument 1: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<string>'
4

5 回答 5

3

bb is just one string, not a collection of them (it's a collection of chars), so you just want to use Add (i.e. files.Add(bb)).

于 2013-11-07T08:58:14.417 回答
2

As specified before you should use Add for a single string.

If you actually want to use AddRange you should create an IEnumerable<string> with your string (for example an array):

files.AddRange(new[]{bb});
于 2013-11-07T09:35:18.900 回答
1

The AddRange() function expects an IEnumerable<T> collection as its argument ( hence the error cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<string>' ) and adds the collection to the end of the specified List<T>. Since you are trying to add a single string, you should use the Add() function :

files.Add(bb);
于 2013-11-07T09:00:51.697 回答
0

AddRange adds a collection of entries to a list, so it's expecting an IEnumerable. You're only supplying a single string, so you should use the Add method.

Try

files.Add(bb);
于 2013-11-07T08:58:55.787 回答
0

To add a String in a List of Strings with addrange you need to have an IEnumerable<string>.

To add a single string to a List of Strings, you have to write in this way:

public List<string> files = new List<string>();
string bb="C:\\cpqsystem";
files.Add(bb);
于 2013-11-07T09:00:32.933 回答