一个提醒...such as implement System.Collections.Generic.IComparer<T>

本文介绍如何在C#中使用委托和LINQ对整数列表进行排序,并展示了自定义比较器的实现方法。同时,文章还讨论了类型转换过程中可能出现的问题及解决方案。
  1. Like this:
    1            L.Sort(delegate(int a, int b)
    2ExpandedBlockStart.gifContractedBlock.gif            {
    3                return (b - a);
    4            }
    );

    The same as: L.Sort((a,b) => b-a);

         If it's not a List<T>
         We can cast it as:

 

1IEnumerable<int> myUsers = al.Cast<int>();

 

Then can use linq to do the things we need with myUsers
But sometimes this may throw exceptions...because not every item can cast to the TResult
Then we can use ofType<T>....
It will ignore the items which can't cast to TResult

2. Also can do it like this:

 

1            DAL demo = new DAL("");
2            L.Sort(demo.Compare);

 


with the DAL below:

 

 1    class DAL : System.Collections.Generic.IComparer<int>
 2ExpandedBlockStart.gifContractedBlock.gif    {
 3ContractedSubBlock.gifExpandedSubBlockStart.gif        IComparer 成员#region IComparer<int> 成员
 4
 5        public int Compare(int x, int y)
 6ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 7            return (y - x);
 8        }

 9
10        #endregion

11     }

 

 

转载于:https://www.cnblogs.com/whoseyourlady/archive/2009/08/17/1547789.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值