简体   繁体   English

List.Sort()引发mscorlib.dll中发生类型为'System.InvalidOperationException'的未处理异常-C#

[英]List.Sort() throws An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll - C#

I have a Person type list and it has some items and I want to sort this list by default(without any params). 我有一个Person类型列表,它有一些项目,并且我想默认对列表进行排序(没有任何参数)。 So I have used person.Sort() method. 所以我用了person.Sort()方法。 When I execute the code I get an exception writes 当我执行代码时,我得到一个异常写

An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll mscorlib.dll中发生了类型为'System.InvalidOperationException'的未处理异常

at Sort() method. 在Sort()方法中。 Any experts please explain the reason behind. 任何专家请解释背后的原因。

List<Person> person = new List<Person>();
person.Add(new Person() { Name = "crank arm", Age = 1234 });
person.Add(new Person() { Name = "chain ring", Age = 1334 });
person.Add(new Person() { Name = "regular seat", Age = 1434 });
person.Add(new Person() { Name = "banana seat", Age = 1444 });
person.Sort();

You need to make your Person class implement IComparable 您需要使您的Person类实现IComparable

The sort methods throws that exception when 排序方法在以下情况时抛出该异常

The default comparer Comparer.Default cannot find an implementation of the IComparable generic interface or the IComparable interface for type T. 默认的比较器Comparer.Default找不到类型T的IComparable通用接口或IComparable接口的实现。

from MSDN MSDN

Person是您定义的类,Sort方法不知道如何在Person对象之间进行比较,您需要实现比较方法,您可以参考: http : //msdn.microsoft.com/zh-cn/library/3da4abas(v = vs.110).aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mscorlib.dll中发生了类型为&#39;System.InvalidOperationException&#39;的未处理异常 - An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll 发生未处理的异常C#的system.data.dll中发生了类型&#39;system.invalidoperationexception&#39;的未处理异常 - An unhandled exception occured an unhandled exception of type 'system.invalidoperationexception' occurred in system.data.dll in c# 在C#中将十进制转换为十六进制,并出现错误“ mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常” - decimal to hexadecimal in c# with mistake “An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll” C# - 实体框架 - mscorlib.dll中发生未处理的“System.StackOverflowException”类型异常 - C# - Entity Framework - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll c# 中的运行时错误 - mscorlib.dll 中发生类型为“System.FormatException”的未处理异常 - Run-time error in c# - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll C#mscorlib.dll中发生未处理的“System.UnauthorizedAccessException”类型异常 - C# An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll C# 中的 System.Data.dll 中发生了“System.InvalidOperationException”类型的未处理异常 - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll in C# System.Data.dll中发生类型为&#39;System.InvalidOperationException&#39;的未处理异常-C#Visual Studio - An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll - C# visual studio mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll中发生了未处理的“System.StackOverflowException”类型异常 - An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM