我有一个名为 Point 的类,它重载“==”和“!=”运算符来比较两个 Point 对象。如何将我的 Point 对象与“null”进行比较,这是一个问题,因为当我使用 null 调用 == 或 != 运算符时,Equals 方法内部会出现问题。请打开一个控制台应用程序,看看我想说什么。我该如何解决。
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public static bool operator == (Point p1,Point p2)
{
return p1.Equals(p2);
}
public static bool operator != (Point p1, Point p2)
{
return !p1.Equals(p2);
}
public override bool Equals(object obj)
{
Point other = obj as Point;
//problem is here calling != operator and this operator calling this …Run Code Online (Sandbox Code Playgroud) 我有一个名为Person的类,它实现了IComparable<int>通用接口.我有一个包含Person对象的通用列表,我将我的列表分配给一个数组,我正在对列表进行排序,但我正在采取以下错误.
错误:{"无法比较数组中的两个元素."}
这是我的Person类
public class Person : IComparable<int>
{
public int Age { get; set; }
public int CompareTo(int other)
{
return Age.CompareTo(other);
}
}
Run Code Online (Sandbox Code Playgroud)
和这个程序cs
class Program
{
static void Main(string[] args)
{
List<Person> list2 = new List<Person>();
list2.Add(new Person() { Age = 80 });
list2.Add(new Person() { Age = 45 });
list2.Add(new Person() { Age = 3 });
list2.Add(new Person() { Age = 77 });
list2.Add(new Person() { Age = 45 });
Person[] array = list2.ToArray(); …Run Code Online (Sandbox Code Playgroud)