C# 2个List<T>比较内部项是否相等(全部相等则相等,反之不相等)

static void Main(string[] args)
{
    List<string> a = new List<string>() { "a", "a", "v", "c", "c" };
    List<string> b = new List<string>() { "v", "c", "a", "a", "c" };
    List<string> c = new List<string>() { "v", "c", "a", "a", "a" };

    Console.WriteLine(a.Equal_EveryItem(b));
    Console.WriteLine(a.Equal_EveryItem(c));
    Console.WriteLine(b.Equal_EveryItem(c));
    Console.ReadKey();
}

 

 


 

调用的方法(作为List的扩展方法)

public static class ListTool
{
    /// <summary>
    /// 判断List<T> a里面的项是否每一个都和List<T> b一致(顺序可以不一样)
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    public static bool Equal_EveryItem<T>(this List<T> a, List<T> b)
    {
        if (a == null || b == null || a.Count <= 0 || b.Count <= 0 || a.Count != b.Count) return false;
        bool result = true;
        foreach (var a_item in a)
        {
            if (b.Exists(b_item => b_item.Equals(a_item)))
            {
                b.Remove(a_item);
            }
            else
            {//不存在
                result = false;
                break;
            }
        }

        return result;
    }

}

 


 

结果:

 

转载于:https://www.cnblogs.com/zhyue93/p/list.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值