Code inspection: Iteration variable can be declared with a more specific type
Last modified: 11 February 2024tip
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
.