C# 字典<;字符串,int>;列出<;字典<;字符串,int>&燃气轮机;在c中#

C# 字典<;字符串,int>;列出<;字典<;字符串,int>&燃气轮机;在c中#,c#,C#,有没有一个干净的方法可以做到这一点 我试过了 List<Dictionary<string, int>> myList = new List<Dictionary<string, int>>(); myList = myDict.ToList(); List myList=new List(); myList=myDict.ToList(); 但这是行不通的,如果可能的话,我正在寻找类似于上面的东西?在你的问题中有两种说法 假设第一个是正确的,

有没有一个干净的方法可以做到这一点

我试过了

List<Dictionary<string, int>> myList = new List<Dictionary<string, int>>();
myList = myDict.ToList();
List myList=new List();
myList=myDict.ToList();

但这是行不通的,如果可能的话,我正在寻找类似于上面的东西?

在你的问题中有两种说法

假设第一个是正确的,然后去做

myList.Add(myDict);
但是如果你的第二句话是正确的,那么你的第一句话应该是正确的

List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>();
List myList=new List();
代码:

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

        myDict.Add("1", 1);
        myDict.Add("2", 2);
        myDict.Add("3", 3);
        myDict.Add("4", 4);
        myDict.Add("5", 5);

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

        myList.Add(myDict);
Dictionary myDict=newdictionary();
myDict.添加(“1”,1);
myDict.添加(“2”,2);
myDict.添加(“3”,3);
myDict.添加(“4”,4);
myDict.添加(“5”,5);
List myList=新列表();
myList.Add(myDict);

像这样的

我想你需要这样的东西:

Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "one");
myDict.Add(2, "two");
myDict.Add(3, "three");

List<KeyValuePair<int, string>> myList = myDict.ToList();

你想让它做什么?请发表一篇文章,而不仅仅是几行引用你没有显示的变量。。。(为什么初始化
myList
只是为了在下一个语句中重新分配它?)@NikhilAgrawal抱歉,我是c#的新手,我是否像
List=myDict.ToList()那样使用它?如果您真的想要
列表
,那么只需执行以下操作:
List meList=new List();meList.Add(医药)。。。但我怀疑你是否想要那样。你想达到什么目的?背景是什么?你有一本什么的字典,想要一张什么的清单?你以后将如何使用该列表?一些示例输入和预期输出将有所帮助。
// example get key and value
var myKey = myList[0].Key;
var myVal = myList[0].Value;