Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

CA1039: Lists are strongly typed







Breaking Change


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

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 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 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.

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.

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.

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. 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]
            return (YourType) ((IList)this)[index];
            ((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);

Community Additions

© 2015 Microsoft