本人还是有点弄不清楚

.Net技术 码拜 8年前 (2016-02-25) 757次浏览
问一下本人标红的地方编程的人怎么知道编程的时候要用这个语句?这是本人疑惑的地方,这和Sort又有什么关联,是不是有内部机制,是不是这个就是语法?记住就行?请给本人详细讲解讲解 谢谢啦!
using System;
using System.Collections;
public class SamplesArrayList
{
public class myReverserClass : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
 return ((new CaseInsensitiveComparer()).Compare(y, x));
        }
}
public static void Main()
{
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add(“The”);
myAL.Add(“quick”);
myAL.Add(“brown”);
myAL.Add(“fox”);
myAL.Add(“jumps”);
myAL.Add(“over”);
myAL.Add(“the”);
myAL.Add(“lazy”);
myAL.Add(“dog”);
// Displays the values of the ArrayList.
Console.WriteLine(“The ArrayList initially contains the following values:”);
PrintIndexAndValues(myAL);
// Sorts the values of the ArrayList using the default comparer.
myAL.Sort();
Console.WriteLine(“After sorting with the default comparer:”);
PrintIndexAndValues(myAL);
// Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort(myComparer);
Console.WriteLine(“After sorting with the reverse case-insensitive comparer:”);
PrintIndexAndValues(myAL);
}
public static void PrintIndexAndValues(IEnumerable myList)
{
int i = 0;
foreach (Object obj in myList)
Console.WriteLine(“\t[{0}]:\t{1}”, i++, obj);
Console.WriteLine();
}
}
解决方案

8

你就记住这是.net内部规定的就行了,里面关系三言两语也说不清楚。具体的你可以百度下,看看官方文档的说明

8

LZ,你这就好像在问,
程序员怎么知道有int,double,float,string 等等的数据类型的?
说白了就是积累的。怎么积累的。看多了,写多了,学多了,人家教的。都是一种积累啊。
没有一本字典能告诉你全部的东西哪来的。本人还是有点弄不清楚

9

MSDN 告诉你 Sort(IComparer) 可以接受一个实现了 IComparer 接口的类,而 IComparer 接口本身就是用于比较大小的
这就是说,你可以按本人的规则进行排序,当然你需要提供比较对象的方法
MSDN 也告诉 CaseInsensitiveComparer 是用于比较对象大小的类
你当然就可以用 CaseInsensitiveComparer 来进行对象的比较了

10

没什么为什么了,假如说本人让你写个冒泡排序你会怎么写?(当然sort内部是快速排序算法,本人只简单的例子)
{4,2,7,9}
你会写
if(a[i]>a[j])
{交换他们}
else if(a[i]<a[j])

不变

else if(a[i]==a[j])
{
不变
}
那个接口的含义无非如此, 两个元素比较大小,当然返回值就-1,0,1 ,3态,
-1小于,0等于,1大于
知道大于,小于,等于了你会排序不?
至于那个new xxx滴东西无非就是这个,他返回大于,小于,等于的判定。
当然你真想知道怎么排,也简单,打开你的教科书翻到快速排序算法,那个就是sort滴基本实现

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明本人还是有点弄不清楚
喜欢 (0)
[1034331897@qq.com]
分享 (0)