How to convert an IEnumerable into a string to put it in a message box in c#? [duplicate]

倖福魔咒の 提交于 2019-12-24 18:46:06

问题


I have an IEnumerable which contains differences of two files. I want to show these differences in a message box.

I tried to convert it with the toArray() method and with the toString() method.

IEnumerable<String> inFirstNotInSecond = file1Lines.Except(file2Lines);
IEnumerable<String> inSecondNotInFirst = file2Lines.Except(file1Lines);

//i also tried this commented line below
//string Diff = new string(inFirstNotInSecond.Take(50).ToArray());

string Diff = inFirstNotInSecond.ToArray().ToString();
string Diff2 = inSecondNotInFirst.ToString();
MessageBox.Show("Difference:" + Diff + Diff2);

When I run the code I don't get the expected differences but rather this: "System.String[]System.Linq.Enumerable+d__73`1[System.String]"


回答1:


List<string> myStrings = new List<string>()
{
    "aaa",
    "bbb",
    "ccc",
};

Console.WriteLine(String.Join(";", myStrings));
// Result:
// aaa;bbb;ccc

Console.WriteLine(String.Join("\n", myStrings));
// Result:
// aaa
// bbb
// ccc


来源:https://stackoverflow.com/questions/57205328/how-to-convert-an-ienumerable-into-a-string-to-put-it-in-a-message-box-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!