V3087. Type of variable enumerated in 'foreach' is not guaranteed to be castable to the type of collection's elements.


The analyzer detected a possible error in a 'foreach' loop. It is very likely that an InvalidCastException will be raised when iterating through the 'IEnumarable<T>' collection.

Consider the following example:

List<object> numbers = new List<object>();
....
numbers.Add(1.0);
....
foreach (int a in numbers)
  Console.WriteLine(a);

In this code, the 'numbers' collection's template is initialized to type 'object', which allows adding objects of any type to it.

It is defined in the loop iterating through this collection that the iterated collection members must be of type 'int'. If an object of another type is found in the collection, it will be cast to the required type, which operation may result in 'InvalidCastException'. In our example, the exception occurs because the value of type 'double', boxed in a collection element of type 'object', cannot be unboxed to type 'int'.

To fix this error, we can cast the collection-template type and the element type in the 'foreach' loop to a single type:

Solution 1:

List<object> numbers = new List<object>();
....
foreach (object a in numbers)

Solution 2:

List<int> numbers = new List<int>();
....
foreach (int a in numbers)

This error can often be observed when working with a collection of base-interface elements while the programmer specifies in the loop the type of one of the interfaces or classes implementing this base interface:

void Foo1(List<ITrigger> triggers){
  ....
  foreach (IOperableTrigger trigger in triggers)
  ....
}
void Foo2(List<ITrigger> triggers){
  ....
  foreach (IMutableTrigger trigger in triggers)
  ....
}

To iterate through the objects of only one particular type in a collection, you can filter them in advance using the 'OfType' function:

void Foo1(List<ITrigger> triggers){
  ....
  foreach (IOperableTrigger trigger in
    triggers.OfType<IOperableTrigger>())
  ....
}
void Foo2(List<ITrigger> triggers){
  ....
  foreach (IMutableTrigger trigger in 
    triggers.OfType<IMutableTrigger>())
  ....
}

This solution guarantees that the 'foreach' loop will iterate only through objects of proper type, making 'InvalidCastException' impossible.


Bugs Found

Checked Projects
354
Collected Errors
13 290