小编Bab*_*abu的帖子

如何将键交换为值和将值转换为字典中的键

伙计们,我想交换字典中的键和值

我的字典看起来像这样

public static void Main(string[] args)
{
Dictionary<int,int> dic = new Dictionary<int,int>{{1,10}, {2, 20}, {3, 30}};
}
Run Code Online (Sandbox Code Playgroud)

现在我要打印值形式字典

 foreach (KeyValuePair<int, int> kvp in dic)
{

    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
            Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)

在展示中我得到了这个

Key = 1, Value = 10
Key = 2, Value = 20
Key = 3, Value = 30
Run Code Online (Sandbox Code Playgroud)

我想在键和值之间交换值..交换答案后应该像这样

Key = 10, Value = 1
Key = 20, Value = 2
Key = 30, Value = 3
Run Code Online (Sandbox Code Playgroud)

我做了lambda表达式,但它改变了,但我需要其他方法来做。

c# swap dictionary

1
推荐指数
2
解决办法
615
查看次数

标签 统计

c# ×1

dictionary ×1

swap ×1