我在 c# 中制作了这个程序,它实现了 IComparable 接口来比较车辆的名称并按字母顺序排序。代码编译成功,但在执行时给了我 StackOverFlowExecption。这是我的代码-
class Vehicle:IComparable
{
private string vehiclename
{
get
{ return vehiclename;
}
set
{
vehiclename = value;
}
}
public Vehicle(string name)
{
vehiclename = name;
}
int IComparable.CompareTo(Object obj)
{
Vehicle temp = (Vehicle)obj;
return string.Compare(this.vehiclename, temp.vehiclename);
}
static void Main(string[] args)
{
Vehicle[] myvehicles = new Vehicle[5];
myvehicles[0] = new Vehicle("Honda City");
myvehicles[1] = new Vehicle("Nano");
myvehicles[2] = new Vehicle("Desire");
myvehicles[3] = new Vehicle("Santro");
myvehicles[4] = new Vehicle("Nissan");
Console.WriteLine("Unordered List of vehicles:");
foreach (Vehicle v in myvehicles)
Console.WriteLine(myvehicles);
Array.Sort(myvehicles);
Console.WriteLine("ordered List of vehicles:");
foreach (Vehicle v in myvehicles)
Console.WriteLine(myvehicles);
}
}
此异常的原因是什么,我该如何解决?