C# 如何将2个对象与Icomparable进行比较
我有这门课C# 如何将2个对象与Icomparable进行比较,c#,C#,我有这门课 { public class Conexiune:ICloneable,IComparable<Conexiune> { [XmlElement("Name")] private string Nume; private string Server; private string User; private string Parola; private string NumeBD; public str
{
public class Conexiune:ICloneable,IComparable<Conexiune>
{
[XmlElement("Name")]
private string Nume;
private string Server;
private string User;
private string Parola;
private string NumeBD;
public string NUME
{
get { return this.Nume; }
set { this.Nume = value; }
}
public string SERVER
{
get { return this.Server; }
set { this.Server = value; }
}
public string USER
{
get { return this.User; }
set { this.User = value; }
}
public string PAROLA
{
get { return this.Parola; }
set { this.Parola = value; }
}
public string NUMEBD
{
get { return this.NumeBD; }
set { this.NumeBD = value; }
}
public override string ToString()
{
return Nume;
}
public Conexiune()
{
}
public Conexiune(string numec, string serverc, string userc, string parolac, string numebdc)
{
this.Nume = numec;
this.Server = serverc;
this.User = userc;
this.Parola = parolac;
this.NumeBD = numebdc;
}
public object CloneP()
{
Conexiune copieP = new Conexiune(this.NUME, this.SERVER, this.USER, this.PAROLA, this.NumeBD);
return copieP;
}
public object Clone()
{
Conexiune copie = new Conexiune();
return copie;
}
//Here i want to compare
public int CompareTo(Conexiune other)
{
int result = 0;
return result;
}
}
}
{
公共类Conexiune:可克隆、可比较
{
[XmlElement(“名称”)]
私有字符串Nume;
私有字符串服务器;
私有字符串用户;
私刑假释;
私有字符串NumeBD;
公共字符串NUME
{
获取{返回this.Nume;}
设置{this.Nume=value;}
}
公共字符串服务器
{
获取{返回此.Server;}
设置{this.Server=value;}
}
公共字符串用户
{
获取{返回this.User;}
设置{this.User=value;}
}
公开字符串假释
{
获取{returnthis.Parola;}
设置{this.Parola=value;}
}
公共字符串NUMEBD
{
获取{返回this.NumeBD;}
设置{this.NumeBD=value;}
}
公共重写字符串ToString()
{
返回Nume;
}
公共委员会()
{
}
public Conexiune(字符串numec、字符串serverc、字符串userc、字符串parolac、字符串numebdc)
{
this.Nume=numec;
this.Server=serverc;
this.User=userc;
这个。帕罗拉=帕罗拉;
this.NumeBD=numebdc;
}
公共对象CloneP()
{
Conexiune copieP=new Conexiune(this.NUME、this.SERVER、this.USER、this.PAROLA、this.NumeBD);
返回复印机;
}
公共对象克隆()
{
Conexiune copie=新Conexiune();
返回副本;
}
//这里我想比较一下
公共内部比较(Conexiune其他)
{
int结果=0;
返回结果;
}
}
}
我想比较两个连接,但不知道用什么来比较。Compare to必须返回int如果要在排序后将对象列高,则返回负数;如果要将其列低,则返回正数;如果对象相等,则返回0
我们不知道您想要的顺序是什么在这种情况下,只需使用字符串比较器作为
NumeBD
字段:
public int CompareTo(Conexiune other)
{
return String.Compare(NUMEBD, other.NUMEBD, StringComparison.Ordinal);
}
如果
NumeBD
可以为null
,您可能需要修改此选项,以便对其进行适当排序如果您不知道,我们应该如何知道?你一定知道为什么要比较它们,什么是相等的或不相等的?另外,您的Clone
方法并不是真正的克隆,这是您的意图吗?我想用NumeBD(数据库名称)来比较它们,