Share via


ICollection implementations have strongly typed members

TypeName

ICollectionImplementationsHaveStronglyTypedMembers

CheckId

CA1035

Category

Microsoft.Design

Breaking Change

Breaking

Cause

A public or protected type implements System.Collections.ICollection but does not provide a strongly typed method for System.Collections.ICollection.CopyTo(System.Array,System.Int32). The strongly typed version of CopyTo must accept two parameters and cannot have a System.Array or an array of System.Object as its first parameter.

Rule Description

This rule requires ICollection implementations to provide strongly typed members so that users are not required to cast arguments to the Object type when using the functionality provided by the interface. This rule assumes that the type that implements ICollection does so to manage a collection of instances of a type that is stronger than Object.

ICollection implements the System.Collections.IEnumerable interface. If the objects in the collection extend System.ValueType, you must provide a strongly typed member for GetEnumerator to avoid the performance loss caused by boxing; this is not required if the collection's objects are a reference type.

To implement a strongly typed version of an interface member, implement the interface members explicitly using names in the form InterfaceName.InterfaceMemberName, such as CopyTo. The explicit interface members use the data types declared by the interface. Implement the strongly typed members using the interface member name, such as CopyTo. Declare the strongly typed members as public, and declare parameters and return values to be of the strong type managed by the collection. The strong types replace weaker types such as Object and Array declared by the interface.

How to Fix Violations

To fix a violation of this rule, implement the interface member explicitly (declare it as CopyTo). Add the public strongly typed member, declared as CopyTo, and have it take a strongly typed array as its first parameter.

When to Exclude Warnings

Exclude a warning from this rule when implementing a new object-based collection, such as a binary tree, where types that extend the new collection determine the strong type. These types should comply with this rule and expose strongly typed members.

Example

The following example demonstrates the correct way to implement ICollection.

using System;
using System.Collections;
namespace DesignLibrary
{
   
   public class ExceptionCollection : ICollection
   {   
      private 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.
      // Because the type underlying this collection is a reference type,
      // you do not need a strongly typed version of GetEnumerator.

      public IEnumerator GetEnumerator()
      {
         return data.GetEnumerator();
      }
   }
}

Enumerators should be strongly typed

Lists are strongly typed

See Also

Reference

System.Array
System.Collections.IEnumerable
System.Collections.ICollection
System.Object