the problem is that ReadAllLines returns a string[]
. If you call ToString
on such an object you get the namespace.classname
as a string. So in your case:
System.String[]
splitting this string results definetely not a number. But in a string[]
with on entry, namely:
System.String[]
If your file has only one line with the space delimited numbers, I would suggest to use File.ReadAllText. It will read the entire content of the file and return it as 1 string
. This way you can use your code almost as it is.
string aresultlist = File.ReadAllText(data);
var bresultlist = aresultlist.Split().Select(s => Convert.ToInt32(s));
List<int> resultlist = bresultlist.ToList();
EDIT:
As suggested by Gilad Green you might have content in the file that cannot be parsed to a number and will throw an exception. To avoid this you can follow this example