gpt4 book ai didi

c# - 在制作 Comparable 类(class)时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:50 26 4
gpt4 key购买 nike

这是我在这里的第一篇文章。我在尝试开设可比较的类(class)时遇到了问题,我希望你能帮助我。

错误:

Error 1 'OutputMasterLibrary.Student' does not implement interface member 'System.Collections.Generic.IComparer.Compare(OutputMasterLibrary.Student, OutputMasterLibrary.Student)''

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OutputMasterLibrary
{
public class Student : IComparable, IComparable<Student>
{
string name { get; set; }
int age { get; set; }
int studentNumber { get; set; }


public Student(string myName, int myAge, int myNumber)
{
name = myName;
age = myAge;
studentNumber = myNumber;
}


public override bool Equals(object obj)
{
Student other = obj as Student;
if (other == null)
{
return false;
}
return (this.name == other.name) && (this.studentNumber == other.studentNumber) && (this.age == other.age);
}


public override int GetHashCode()
{
return name.GetHashCode() + studentNumber.GetHashCode() + age.GetHashCode();
}
}
}

最佳答案

错误消息准确说明了您缺少的内容。

在你的Student类中实现

public int Compare(Student student1, Student student2)

关于c# - 在制作 Comparable 类(class)时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22857924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com