你好,我把那个Collection 改为List了,没提示出错了,下面的是例子中实现GetEnumerator() 的代码 public IEnumerator<T> GetEnumerator() { return (innerCollection as IEnumerable<T>).GetEnumerator(); }为什么会提示 错误 1 “test13.ShortCollections<T>”不实现接口成员“System.Collections.IEnumerable.GetEnumerator()”。“test13.ShortCollections<T>.GetEnumerator()”无法实现“System.Collections.IEnumerable.GetEnumerator()”,因为它没有匹配的返回类型“System.Collections.IEnumerator”。
IEnumerable.IEnumerator GetEnumerator() { return (innerCollection as IEnumerable).GetEnumerator(); }
提示 错误 类型“System.Collections.IEnumerable”中不存在类型名称“IEnumerator” 这是完整代码using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace test13 { public class ShortCollections<T> : IList<T> { protected List<T> innerCollection; protected int maxSize = 10; public ShortCollections() : this(10) { } public ShortCollections(int size) { maxSize = size; innerCollection = new List<T>(); } public ShortCollections(List<T> list) : this(10, list) { } public ShortCollections(int size, List<T> list) { maxSize = size; if (list.Count <= maxSize) { innerCollection = new List<T>(list); } else ThrowTooManyItemsException(); } protected void ThrowTooManyItemsException() { throw new IndexOutOfRangeException("Unable to add any more items,maximum size is " + maxSize.ToString() + " items."); } public int IndexOf(T item) { return (innerCollection as IList<T>).IndexOf(item); } public void Insert(int index, T item) { if (Count < maxSize) { (innerCollection as IList<T>).Insert(index, item); } else { ThrowTooManyItemsException(); } } public void RemoveAt(int index) { (innerCollection as IList<T>).RemoveAt(index); } public T this[int index] { get { return (innerCollection as IList<T>)[index]; } set { (innerCollection as IList<T>)[index] = value; } } public void Add(T item) { if (Count < maxSize) { (innerCollection as ICollection<T>).Add(item); } else { ThrowTooManyItemsException(); } } public void Clear() { (innerCollection as ICollection<T>).Clear(); } public bool Contains(T item) { return (innerCollection as ICollection<T>).Contains(item); } public void CopyTo(T[] array, int arrayIndex) { (innerCollection as ICollection<T>).CopyTo(array, arrayIndex); } public int Count { get { return (innerCollection as ICollection<T>).Count; } } public bool IsReadOnly { get { return (innerCollection as ICollection<T>).IsReadOnly; } } public bool Remove(T item) { return (innerCollection as ICollection<T>).Remove(item); } public IEnumerable.IEnumerator GetEnumerator() { return (innerCollection as IEnumerable ).GetEnumerator(); } } }
IEnumerator IEnumerable.GetEnumerator() { return (innerCollection as IEnumerable).GetEnumerator(); }写错了
写了这2个以后,不报错了,但还是不是很明白,是不是需要这样写的时候是为了“实现具有相同名称和签名的两个接口方法” public IEnumerator<T> GetEnumerator() { return (innerCollection as IEnumerable<T>).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
比如说 class A { public int i; } class Program { Func<int> f; void foo() { A a = new A(); a.i = 1; // 按理说a在出了foo后就可以销毁了。但是如果你这么写: f = () => a.i; // 那么a这个对象直到program销毁它才被销毁 } }
额,原来没那个啊,原文的确是IList,因为出现了那个错误,我看它代码写protected Collection<T> innerCollection;
于是我改为ICollection 是不是它代码错了啊,protected Collection<T> innerCollection; 改为protected List<T> innerCollection; ???
谢谢
额,原来没那个啊,原文的确是IList,因为出现了那个错误,我看它代码写protected Collection<T> innerCollection;
于是我改为ICollection 是不是它代码错了啊,protected Collection<T> innerCollection; 改为protected List<T> innerCollection; ???
谢谢
是的,没那个的,修改下就可以,还有下面
innerCollection = new Collection<T>();
也改为
innerCollection = new List<T>();
你好,我把那个Collection 改为List了,没提示出错了,下面的是例子中实现GetEnumerator()
的代码 public IEnumerator<T> GetEnumerator()
{
return (innerCollection as IEnumerable<T>).GetEnumerator();
}为什么会提示
错误 1 “test13.ShortCollections<T>”不实现接口成员“System.Collections.IEnumerable.GetEnumerator()”。“test13.ShortCollections<T>.GetEnumerator()”无法实现“System.Collections.IEnumerable.GetEnumerator()”,因为它没有匹配的返回类型“System.Collections.IEnumerator”。
{
return (innerCollection as IEnumerable).GetEnumerator();
}
提示 错误 类型“System.Collections.IEnumerable”中不存在类型名称“IEnumerator” 这是完整代码using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace test13
{
public class ShortCollections<T> : IList<T>
{
protected List<T> innerCollection;
protected int maxSize = 10; public ShortCollections()
: this(10)
{
} public ShortCollections(int size)
{
maxSize = size;
innerCollection = new List<T>();
} public ShortCollections(List<T> list)
: this(10, list)
{
} public ShortCollections(int size, List<T> list)
{
maxSize = size;
if (list.Count <= maxSize)
{
innerCollection = new List<T>(list);
}
else
ThrowTooManyItemsException();
} protected void ThrowTooManyItemsException()
{
throw new IndexOutOfRangeException("Unable to add any more items,maximum size is " + maxSize.ToString() + " items.");
} public int IndexOf(T item)
{
return (innerCollection as IList<T>).IndexOf(item);
} public void Insert(int index, T item)
{
if (Count < maxSize)
{
(innerCollection as IList<T>).Insert(index, item);
}
else
{
ThrowTooManyItemsException();
}
} public void RemoveAt(int index)
{
(innerCollection as IList<T>).RemoveAt(index);
} public T this[int index]
{
get
{
return (innerCollection as IList<T>)[index];
}
set
{
(innerCollection as IList<T>)[index] = value;
}
} public void Add(T item)
{
if (Count < maxSize)
{
(innerCollection as ICollection<T>).Add(item);
}
else
{
ThrowTooManyItemsException();
}
} public void Clear()
{
(innerCollection as ICollection<T>).Clear();
} public bool Contains(T item)
{
return (innerCollection as ICollection<T>).Contains(item);
} public void CopyTo(T[] array, int arrayIndex)
{
(innerCollection as ICollection<T>).CopyTo(array, arrayIndex);
} public int Count
{
get
{
return (innerCollection as ICollection<T>).Count;
}
} public bool IsReadOnly
{
get
{
return (innerCollection as ICollection<T>).IsReadOnly;
}
} public bool Remove(T item)
{
return (innerCollection as ICollection<T>).Remove(item);
} public IEnumerable.IEnumerator GetEnumerator()
{
return (innerCollection as IEnumerable ).GetEnumerator();
}
}
}
{
return (innerCollection as IEnumerable).GetEnumerator();
}写错了
”我想请问一下,因为支持了IList<T>,而IList继承了IEnumerable,所以要实现GetEnumerator这个方法对不对?
百度到一个
“要实现System.Collections.Generic.IEnumerable<T>接口比较麻烦,这个接口有一个方法:GetEnumerator(),不过有两个重载版本
* 第一:System.Collections.Generic.IEnumerator<T> GetEnumerator(),
* 第二:System.Collections.IEnumerator GetEnumerator()...(注意这里的重载是以返回值为区别的,其实不然,这只是接口而已,还没有去实现).
* 那么如何去实现这个方法呢?
* 如下:必须同时有方法一、方法二,缺一不可.
* 如果缺了方法一,编译错误:不实现接口成员"System.Collections.Generic.IEnumerable<string> GetEnumerator()",
* 如果缺了方法二,编译错误:不实现接口成员"System.Collections.IEnumerable GetEnumerator()",因为它没有匹配的返回类型
* "System.Collections.IEnumerator",所以两个方法缺一不可,而且方法二必须是显式实现接口”
{
return (innerCollection as IEnumerable<T>).GetEnumerator();
} IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
class A
{
public int i;
}
class Program
{
Func<int> f;
void foo()
{
A a = new A();
a.i = 1; // 按理说a在出了foo后就可以销毁了。但是如果你这么写:
f = () => a.i;
// 那么a这个对象直到program销毁它才被销毁
}
}