交换清单<>用C#使用LINQ元素 [英] Swap List<> elements with c# using LINQ
本文介绍了交换清单<>用C#使用LINQ元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个名单
无功名单=新名单{3,1,0,5};
var list = new List { 3, 1, 0, 5 };
我想交换元件0与2
输出
0,1,3,5
output 0, 1, 3, 5
推荐答案
如果您只是希望它排序,我会使用List.Sort()。
If you just want it sorted, I'd use List.Sort().
如果您想要交换,没有内置的方法来做到这一点。这将会是很容易写一个扩展方法,虽然:
If you want to swap, there is no built in method to do this. It'd be easy to write an extension method, though:
static void Swap<T>(this List<T> list, int index1, int index2)
{
T temp = list[index1];
list[index1] = list[index2];
list[index2] = temp;
}
然后你可以这样做:
You could then do:
list.Swap(0,2);
这篇关于交换清单<>用C#使用LINQ元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文