CA1038: Enumerators should be strongly typed
|
TypeName |
EnumeratorsShouldBeStronglyTyped |
|
CheckId |
CA1038 |
|
Category |
Microsoft.Design |
|
Breaking Change |
Breaking |
A public or protected type implements System.Collections.IEnumerator but does not provide a strongly typed version of the IEnumerator.Current property. Types that are derived from the following types are exempt from this rule:
This rule requires IEnumerator implementations to also provide a strongly typed version of the Current property so that users are not required to cast the return value to the strong type when they use the functionality that is provided by the interface. This rule assumes that the type that implements IEnumerator contains a collection of instances of a type that is stronger than Object.
The following example demonstrates the correct way to implement a strongly typed IEnumerator type.
using System; using System.Collections; namespace DesignLibrary { // The ExceptionEnumerator class implements a strongly typed enumerator // for the ExceptionCollection type. public class ExceptionEnumerator: IEnumerator { private IEnumerator myCollectionEnumerator; private ExceptionEnumerator () {} public ExceptionEnumerator(ExceptionCollection collection) { myCollectionEnumerator = collection.data.GetEnumerator(); } // Implement the IEnumerator interface member explicitly. object IEnumerator.Current { get { return myCollectionEnumerator.Current; } } // Implement the strongly typed member. public Exception Current { get { return (Exception) myCollectionEnumerator.Current; } } // Implement the remaining IEnumerator members. public bool MoveNext () { return myCollectionEnumerator.MoveNext(); } public void Reset () { myCollectionEnumerator.Reset(); } } public class ExceptionCollection : ICollection { internal ArrayList data; ExceptionCollection() { data = new ArrayList(); } // Provide the explicit interface member for ICollection. void ICollection.CopyTo(Array array, int index) { data.CopyTo(array, index); } // Provide the strongly typed member for ICollection. public void CopyTo(Exception[] array, int index) { ((ICollection)this).CopyTo(array, index); } // Implement the rest of the ICollection members. public int Count { get { return data.Count; } } public object SyncRoot { get { return this; } } public bool IsSynchronized { get { return false; } } // The IEnumerable interface is implemented by ICollection. IEnumerator IEnumerable.GetEnumerator() { return new ExceptionEnumerator(this); } public ExceptionEnumerator GetEnumerator() { return new ExceptionEnumerator(this); } } }