Bagikan melalui


Lists are strongly typed

TypeName

ListsAreStronglyTyped

CheckId

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

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 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 does this to manage a collection of instances of a type that is 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 caused by boxing; this is not required if the collection's objects are a reference type.

To comply with this rule, implement the interface members explicitly using names in the form InterfaceName.InterfaceMemberName, such as Add. The explicit interface members use the data types declared by the interface. Implement the strongly typed members 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 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, explicitly implement IList members and provide strongly typed alternatives for the members noted previously. For code that correctly implements the IList interface and provides the required strongly typed members, see the following example.

When to Exclude Warnings

Exclude a warning from this rule when implementing 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 should all strongly typed collections. Note that CollectionBase provides the explicit implementation of the IList interface for you, so you need 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);
      }
   }
}

ICollection implementations have strongly typed members

Enumerators should be strongly typed

See Also

Reference

System.Collections.CollectionBase
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IList
System.Object