交换两个项目的名单,LT; T> [英] Swap two items in List<T>

查看:97
本文介绍了交换两个项目的名单,LT; T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种办法LINQ交换的列表&LT内部两个项目的位置; T>

Is there a LINQ way to swap the position of two items inside a list<T>?

推荐答案

从的 C#:好/最好的实现Swap方法

public static void Swap(IList<int> list, int indexA, int indexB)
{
    int tmp = list[indexA];
    list[indexA] = list[indexB];
    list[indexB] = tmp;
}

可以LINQ-I-田间像

which can be linq-i-fied like

public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)
{
    T tmp = list[indexA];
    list[indexA] = list[indexB];
    list[indexB] = tmp;
    return list;
}


var lst = new List<int>() { 8, 3, 2, 4 };
lst = lst.Swap(1, 2);

这篇关于交换两个项目的名单,LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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