CA1010: Collections should implement generic interface
Visual Studio 2012
|
TypeName |
CollectionsShouldImplementGenericInterface |
|
CheckId |
CA1010 |
|
Category |
Microsoft.Design |
|
Breaking Change |
Non-breaking |
An externally visible type implements the System.Collections.IEnumerable interface but does not implement the System.Collections.Generic.IEnumerable<T> interface, and the containing assembly targets .NET Framework 2.0. This rule ignores types that implement System.Collections.IDictionary.
using System; using System.Collections; namespace Samples { public class Book { public Book() { } } public class BookCollection : CollectionBase { public BookCollection() { } public void Add(Book value) { InnerList.Add(value); } public void Remove(Book value) { InnerList.Remove(value); } public void Insert(int index, Book value) { InnerList.Insert(index, value); } public Book this[int index] { get { return (Book)InnerList[index]; } set { InnerList[index] = value; } } public bool Contains(Book value) { return InnerList.Contains(value); } public int IndexOf(Book value) { return InnerList.IndexOf(value); } public void CopyTo(Book[] array, int arrayIndex) { InnerList.CopyTo(array, arrayIndex); } } }
using System; using System.Collections; using System.Collections.Generic; namespace Samples { public class Book { public Book() { } } public class BookCollection : CollectionBase, IList<Book> { public BookCollection() { } int IList<Book>.IndexOf(Book item) { return this.List.IndexOf(item); } void IList<Book>.Insert(int location, Book item) { } Book IList<Book>.this[int index] { get { return (Book) this.List[index]; } set { } } void ICollection<Book>.Add(Book item) { } bool ICollection<Book>.Contains(Book item) { return true; } void ICollection<Book>.CopyTo(Book[] array, int arrayIndex) { } bool ICollection<Book>.IsReadOnly { get { return false; } } bool ICollection<Book>.Remove(Book item) { if (InnerList.Contains(item)) { InnerList.Remove(item); return true; } return false; } IEnumerator<Book> IEnumerable<Book>.GetEnumerator() { return new BookCollectionEnumerator(InnerList.GetEnumerator()); } private class BookCollectionEnumerator : IEnumerator<Book> { private IEnumerator _Enumerator; public BookCollectionEnumerator(IEnumerator enumerator) { _Enumerator = enumerator; } public Book Current { get { return (Book)_Enumerator.Current; } } object IEnumerator.Current { get { return _Enumerator.Current; } } public bool MoveNext() { return _Enumerator.MoveNext(); } public void Reset() { _Enumerator.Reset(); } public void Dispose() { } } } }