CA1035: 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 ICollection.CopyTo. 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 they use the functionality that is 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 decrease in performance that is caused by boxing. This is not required when the objects of the collection are a reference type.

To implement a strongly typed version of an interface member, implement the interface members explicitly by using names in the form InterfaceName.InterfaceMemberName, such as CopyTo. The explicit interface members use the data types that are declared by the interface. Implement the strongly typed members by 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 that is managed by the collection. The strong types replace weaker types such as Object and Array that are 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 Suppress Warnings

Suppress a warning from this rule if you implement 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();
      }
   }
}

CA1038: Enumerators should be strongly typed

CA1039: Lists are strongly typed

See Also

Reference

System.Array

System.Collections.IEnumerable

System.Collections.ICollection

System.Object