ReSharper
 
Get ReSharper
You are viewing the documentation for an earlier version of ReSharper.

Code Inspection: Iteration variable can be declared with a more specific type

Last modified: 21 July 2022

Consider the following class hierarchy:

If we wanted to write a method that would print all of children’s names, we could define the following:

However, why should our iteration variable be Person? In fact, we could easily change it to Child and still get the same result. Note that there is a case when a more general type could not be replaced with a derived one without changes to the way code behaves. This case can occur if your iteration variable is declared to be of type dynamic.