如何将IEnumerable转换为字符串以将其放入C#的消息框中? [英] How to convert an IEnumerable into a string to put it in a message box in c#?

查看:595
本文介绍了如何将IEnumerable转换为字符串以将其放入C#的消息框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IEnumerable,其中包含两个文件的差异.我想在消息框中显示这些差异.

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

我尝试使用toArray()方法和toString()方法将其转换.

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

运行代码时,我没有得到预期的差异,而是这样:"System.String [] System.Linq.Enumerable + d__73`1 [System.String]"

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]"

推荐答案

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

这篇关于如何将IEnumerable转换为字符串以将其放入C#的消息框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆