比较
is运算符
is运算符是用来检查对象是不是给定类型,或是否转换为给定类型,返回值为bool值
主要语法如下
bool test1 = 10.GetType() == typeof(int) <operand> is <type>
运算符的重载
运算符重载需使用静态关键字static和operator关键字和运算符本身
能重载的运算符
一元运算符:+,-,!,~,++,--,true,false
二元运算符:+,-,*,/,%,&,|,^,<<,>>
比较运算符:==,!=,<,>,<=,>=
class AddClass1 { public int val; public static AddClass1 operator +(AddClass1 op1,AddClass1 op2) { AddClass1 returnVal = new AddClass1(); returnVal.val = op1.val + op2.val; return returnVal; } public static AddClass1 operator -(AddClass1 op1,AddClass1 op2) { AddClass1 returnVal = new AddClass1(); returnVal.val = op1.val - op2.val; return returnVal; } }
IComparable和IComparer接口
IComparable在要比较的对象的类中实现,可以比较该对象和另一个对象
IComparer在一个单独的类中实现,可以比较任以两个对象
IComparable提供一个方法Compare To(),IComparer也提供一个方法Compare()
IComparable:
class Person : IComparable { public string Name; public int Age; public Person(string name, int age) { this.Name = name; this.Age = age; } public int CompareTo(object obj) { if (obj is Person) { Person otherPerson = obj as Person; return this.Age - otherPerson.Age; } else throw new ArgumentException ("Object to compare to is not a person object"); } }
Person TT = new Person("TT",23); Person LL = new Person("LL",22); TT.CompareTo(LL);
IComparer:
IComparer在使用时需先定义静态的Default,并使用comparer.Default获取一个实例
class PersonComparerName : IComparer { public static IComparer Default = new PersonComparerName(); public int Compare(object x, object y) { if (x is Person && y is Person) { return Comparer.Default.Compare (((Person)x).Name, ((Person)y).Name); } else throw new ArgumentException ("One or both objects to compare are not Person objects"); } }
Person TT = new Person("TT", 23); Person LL = new Person("LL", 22); PersonComparerName.Default.Compare(TT, LL);
此处返回值为1,是按照从开始的字母顺序来比较的,若TT<LL,则返回值为-1
对集合排序
对集合排序使用的是集合的sort()方法
ArrayList list = new ArrayList(); list.Add(new Person("TT", 29)); list.Add(new Person("LL", 22)); list.Add(new Person("Xb", 36)); list.Add(new Person("Xh", 33)); //排序前集合内顺序 for (int i = 0; i < list.Count; i++) Console.WriteLine($"{(list[i] as Person).Name}({(list[i] as Person).Age})"); Console.WriteLine(); Console.WriteLine("People sorted with default comparer (by age):"); //不给Sort()传入参数 list.Sort(); for (int i = 0; i < list.Count; i++) Console.WriteLine($"{(list[i] as Person).Name}({(list[i] as Person).Age})"); Console.WriteLine(); Console.WriteLine("People sorted with nondefault comparer (by name):"); //给Sort()传入参数 list.Sort(PersonComparerName.Default); for (int i = 0; i < list.Count; i++) Console.WriteLine($"{(list[i] as Person).Name}({(list[i] as Person).Age})");
当使用默认的Sort()方法时,必须在类中使用IComparable接口并定义CompareTo()方法,排序的默认方法为CompareTo()方法
当给Sort()方法传入Default时,排序的方法为Compare()方法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器