[英]Error message regarding IEnumerable.GetEnumerator()
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(). 以下是具有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();
}
}
As others have said, you need to implement IEnumerable
as well as IEnumerable<T>
. 正如其他人所说,你需要实现
IEnumerable
以及IEnumerable<T>
。 However, since IEnumberable<T>
itself implemets IEnumerable
this is trivial, just call your generic GetEnumerator()
: 但是,由于
IEnumberable<T>
本身实现了IEnumerable
这是微不足道的,只需调用你的通用GetEnumerator()
:
public class ShortCollection<T> : IList<T>
{
protected Collection<T> innerCollection;
protected int maxSize = 10;
public IEnumerator<T> GetEnumerator()
{
return innerCollection.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
I'm assuming that you have methods for actually adding and removing from the innerCollection
and just left them out for brevity since they didn't relate to the question at hand. 我假设您有实际添加和删除
innerCollection
方法,为了简洁而将它们排除在外,因为它们与手头的问题无关。
IEnumerable
and IEnumerable<T>
are different interfaces and your code has to implement both. IEnumerable
和IEnumerable<T>
是不同的接口,你的代码必须同时实现。 Your code only implements the GetEnumerator() of IEnumerable<T>
, but not GetEnumerator() of IEnumerable
. 您的代码仅实现
IEnumerable<T>
GetEnumerator(),但不实现IEnumerable
GetEnumerator()。 You should consider installing ReSharper which makes it easy to fix errors like this one. 你应该考虑安装ReSharper,它可以很容易地修复像这样的错误。
You need to implement a few more things: 你需要实现更多的东西:
public class ShortCollection<T> : IList<T>
{
protected Collection<T> innerCollection;
protected int maxSize = 10;
#region IList<T> Members
public int IndexOf(T item)
{
return innerCollection.IndexOf(item);
}
public void Insert(int index, T item)
{
innerCollection.Insert(index, item);
}
public void RemoveAt(int index)
{
innerCollection.RemoveAt(index);
}
public T this[int index]
{
get
{
return innerCollection[index];
}
set
{
innerCollection[index] = value;
}
}
#endregion
#region ICollection<T> Members
public void Add(T item)
{
innerCollection.Add(item);
}
public void Clear()
{
innerCollection.Clear();
}
public bool Contains(T item)
{
return innerCollection.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
innerCollection.CopyTo(array, arrayIndex);
}
public int Count
{
get { return innerCollection.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
return innerCollection.Remove(item);
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
return innerCollection.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return innerCollection.GetEnumerator();
}
#endregion
}
This code will compile... :) 这段代码会编译...... :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.