Partager via


CA1039: Lists are strongly typed

Item Value
RuleId CA1039
Category Microsoft.Design
Breaking change Breaking

Cause

The public or protected type implements System.Collections.IList but does not provide a strongly typed method for one or more of the following:

  • IList.Item

  • IList.Add

  • IList.Contains

  • IList.IndexOf

  • IList.Insert

  • IList.Remove

Note

This rule has been deprecated. For more information, see Deprecated rules.

Rule description

This rule requires IList implementations to provide strongly typed members, so that users are not required to cast arguments to the System.Object type when they use the functionality that is provided by the interface. The IList interface is implemented by collections of objects that can be accessed by index. This rule assumes that the type that implements IList manages a collection of instances of a type that's stronger than Object.

IList implements the System.Collections.ICollection and System.Collections.IEnumerable interfaces. If you implement IList, you must provide the required strongly typed members for ICollection. 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 comply with this rule, implement the interface members explicitly by using names in the form InterfaceName.InterfaceMemberName, such as Add. 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 Add. 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, explicitly implement IList members and provide strongly typed alternatives for the members that were noted previously. For code that correctly implements the IList interface and provides the required strongly typed members, see the following example.

When to suppress warnings

Suppress a warning from this rule when you implement a new object-based collection, such as a linked list, where types that extend the new collection determine the strong type. These types should comply with this rule and expose strongly typed members.

Example

In the following example, the type YourType extends System.Collections.CollectionBase, as all strongly typed collections should. CollectionBase provides the explicit implementation of the IList interface for you. Therefore, you must only provide the strongly typed members for IList and ICollection.

using System;
using System.Collections;
namespace DesignLibrary
{
   public class YourType
   {
      // Implementation for your strong type goes here.
      
      public YourType() {}
   }

   public class YourTypeCollection : CollectionBase
   {
      // Provide the strongly typed members for IList.
      public YourType this[int index]
      {
         get 
         {
            return (YourType) ((IList)this)[index];
         }
         set 
         {
            ((IList)this)[index] =  value;
         }
      }

      public int Add(YourType value)
      {
         return ((IList)this).Add ((object) value);
      }

      public bool Contains(YourType value) 
      {
         return ((IList)this).Contains((object) value);
      }

      public void Insert(int index, YourType value) 
      {
         ((IList)this).Insert(index, (object) value);
      }

      public void Remove(YourType value) 
      {
         ((IList)this).Remove((object) value);
      }

      public int IndexOf(YourType value) 
      {
         return ((IList)this).IndexOf((object) value);
      }

      // Provide the strongly typed member for ICollection.

      public void CopyTo(YourType[] array, int index)
      {
         ((ICollection)this).CopyTo(array, index);
      }
   }
}

CA1035: ICollection implementations have strongly typed members

CA1038: Enumerators should be strongly typed

See also