C# 连接由逗号分隔的字典键';s

C# 连接由逗号分隔的字典键';s,c#,string,dictionary,concatenation,comma,C#,String,Dictionary,Concatenation,Comma,我正在寻找连接字典键的更好方法,这就是我现在正在做的: Dictionary<int, string> myList = new Dictionary<int, string>(); myList.Add(1, "value"); myList.Add(2, "value"); myList.Add(3, "value"); myList.Add(4, "value"); string choices = ""; foreach (int key in myList

我正在寻找连接字典键的更好方法,这就是我现在正在做的:

Dictionary<int, string> myList = new Dictionary<int, string>();

myList.Add(1, "value");
myList.Add(2, "value");
myList.Add(3, "value");
myList.Add(4, "value");

string choices = "";

foreach (int key in myList.Keys)
{
    choices += key + " ";
}

choices = "(" + choices.Trim().Replace(" ", ",") + ")"; // (1,2,3,4)
Dictionary myList=newdictionary();
增加(1,“价值”);
添加(2,“价值”);
添加(3,“价值”);
添加(4,“价值”);
字符串选择=”;
foreach(myList.Keys中的int键)
{
选项+=键+“”;
}
choices=“(“+choices.Trim().Replace”(“,”,“+”)”);//(1,2,3,4)
我相信有一种更好的方法,也许是使用LINQ?

您可以使用:

Dictionary<int, string> myList = new Dictionary<int, string>();

myList.Add(1, "value");
myList.Add(2, "value");
myList.Add(3, "value");
myList.Add(4, "value");

choices = String.Format("({0})", string.Join(",", myList.Keys));
Dictionary myList=newdictionary();
增加(1,“价值”);
添加(2,“价值”);
添加(3,“价值”);
添加(4,“价值”);
choices=String.Format(“({0})”,String.Join(“,”,myList.Keys));
string.Format("({0})", string.Join(",", myList.Keys));