关于IEnumerable.GetEnumerator(错误消息) [英] Error message regarding IEnumerable.GetEnumerator()

查看:534
本文介绍了关于IEnumerable.GetEnumerator(错误消息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误消息,我想不通为什么

 错误1'Exo5Chap12.ShortCollection<!T> 不实现接口成员
'System.Collections.IEnumerable.GetEnumerator()。
'Exo5Chap12.ShortCollection< T> .GetEnumerator()无法实现
'System.Collections.IEnumerable.GetEnumerator()',因为它不具备系统的匹配
返回类型。 Collections.IEnumerator。
E:\MyFolders\Dev\c#\Chapter12\Exo5Chap12\Exo5Chap12\exo5.cs 9月18日Exo5Chap12

下面是的GetEnumerator的实现()的代码。 ?
什么是错的。



 公共类ShortCollection< T> :IList的< T> 
{
保护收藏< T> innerCollection;
保护INT MAXSIZE = 10;
公众的IEnumerator< T>的GetEnumerator()
{
回报(innerCollection为IEnumerator的< T>)的GetEnumerator();
}
}


解决方案

的IEnumerable 的IEnumerable< T> 是不同的接口,你的代码必须实现两者。您的代码只实现的的GetEnumerator()的IEnumerable< T> ,但不是的GetEnumerator(的IEnumerable )的。你应该考虑安装ReSharper的,这使得它很容易解决象这样的错误之一。


I get this error message and I can't figure out why!

Error   1   'Exo5Chap12.ShortCollection<T>' does not implement interface member 
'System.Collections.IEnumerable.GetEnumerator()'. 
'Exo5Chap12.ShortCollection<T>.GetEnumerator()' cannot implement 
'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching 
return type of 'System.Collections.IEnumerator'.    
E:\MyFolders\Dev\c#\Chapter12\Exo5Chap12\Exo5Chap12\exo5.cs 9   18  Exo5Chap12

Here is the code with an implementation of GetEnumerator(). What is wrong?

 public class ShortCollection<T> : IList<T>
{
    protected Collection<T> innerCollection;
    protected int maxSize = 10;
    public IEnumerator<T> GetEnumerator()
    {
        return (innerCollection as IEnumerator<T>).GetEnumerator();
    }
}

解决方案

IEnumerable and IEnumerable<T> are different interfaces and your code has to implement both. Your code only implements the GetEnumerator() of IEnumerable<T>, but not GetEnumerator() of IEnumerable. You should consider installing ReSharper which makes it easy to fix errors like this one.

这篇关于关于IEnumerable.GetEnumerator(错误消息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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