错误:错误1无法将类型'void'隐式转换为'System.Collections.Generic.IComparer< IPerson>' .................... [英] Error : Error 1 Cannot implicitly convert type 'void' to 'System.Collections.Generic.IComparer<IPerson>' ....................

查看:150
本文介绍了错误:错误1无法将类型'void'隐式转换为'System.Collections.Generic.IComparer< IPerson>' ....................的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ///   <  摘要 >  
/// 创建一个实现IComparer的新类(Of IPerson)
/ // 这个类应该比较IPerson对象的Name属性,这样对象的排序方式如下:
/// Alice,Bob,Charles
// / < / summary >

public IComparer< iperson> GetPeopleNameComparer()
{
// 抛出新的NotImplementedException(删除此行并编写一些代码!);

列表< iperson> list = new 列表< iperson>();
list.Add( new Employee(){Name = F,DOB = DateTime.ParseExact( 01-01-2001 dd-MM-yyyy,CultureInfo.InvariantCulture)});
list.Add( new Employee(){Name = G,DOB = DateTime.ParseExact( 01-01-2002 dd-MM-yyyy,CultureInfo.InvariantCulture)});
list.Add( new Employee(){Name = H,DOB = DateTime.ParseExact( 01-01-2009 dd-MM-yyyy,CultureInfo.InvariantCulture)});
list.Add( new Employee(){Name = A,DOB = DateTime.ParseExact( 01-01-2003 dd-MM-yyyy,CultureInfo.InvariantCulture)});
list.Add( new Employee(){Name = V,DOB = DateTime.ParseExact( 01-01-2004 dd-MM-yyyy,CultureInfo.InvariantCulture)});
list.Add( new Employee(){Name = B,DOB = DateTime.ParseExact( 01-01-2005 dd-MM-yyyy,CultureInfo.InvariantCulture)});
return list.Sort(); // 我在这里收到错误。任何人都可以帮助我。
}

解决方案

列表< iperson> list =  new 列表< iperson>(); 



这里您正在创建< list>在iperson类型中,但在向列表中添加值时,您将分配Employee类型的值。然后你在列表上调用sort()。此解决方案将iperson类型值添加到列表并调用sort()。


嗯 - 再次读取错误消息...

您的方法签名要求为了返回一个IComparer for iperson list.Sort()方法什么都不返回(void)..

你在这里做的是创建和排序你没有获得的List或创建一个Comparer! />
但是,您可以为Sort方法提供您想要使用的比较器。

据我了解你的要求,你应该返回一个特殊的Comparer实现,而不是创建一个列表,也不要对它进行排序。

http://msdn.microsoft.com/de-de/library/cfttsh47(v=vs.110).aspx [ ^ ]

你找到一个如何在那里实现和使用不同比较器的例子。


你必须在排序后调用.ToList,因为sort不会返回值。

/// <summary>
/// Create a new class that implements IComparer(Of IPerson)
/// This class should compare the Name property of IPerson objects, such that objects are ordered like: 
/// "Alice", "Bob", "Charles"
/// </summary> 

public IComparer<iperson> GetPeopleNameComparer()
{
 //throw new NotImplementedException("Remove this line and write some code!");

            List<iperson> list = new List<iperson>();
            list.Add(new Employee() { Name = "F", DOB = DateTime.ParseExact("01-01-2001", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "G", DOB = DateTime.ParseExact("01-01-2002", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "H", DOB = DateTime.ParseExact("01-01-2009", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "A", DOB = DateTime.ParseExact("01-01-2003", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "V", DOB = DateTime.ParseExact("01-01-2004", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            list.Add(new Employee() { Name = "B", DOB = DateTime.ParseExact("01-01-2005", "dd-MM-yyyy", CultureInfo.InvariantCulture) });
            return list.Sort(); // Im getting error here.Can anyone please help me.
}

解决方案

List<iperson> list = new List<iperson>();


here you are creating <list> Of type iperson but while adding values to the list you are assigning values of Employee type. Then you are calling sort() on the list. Solution for this add iperson type values to the list and the call sort().


Hmm - read the error message again...
Your method signature requires to return an IComparer for iperson the list.Sort() method returns nothing (void)..
What you do here is creating and sorting a List you don't obtain or create a Comparer!
Though, You can give the Sort method a comparer you want to use.
As far as I understand your requirement you should return a special Comparer implementation not create a list nor sort it.
Read http://msdn.microsoft.com/de-de/library/cfttsh47(v=vs.110).aspx[^]
You find an example how to implement and use different Comparers there.


You have to call .ToList after sort since sort doesn't return a value.


这篇关于错误:错误1无法将类型'void'隐式转换为'System.Collections.Generic.IComparer&lt; IPerson&gt;' ....................的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆