Code Inspection: Return type can be IEnumerable<T> (non-private accessibility)
If a method returns a more generic type, it allows for more flexibility. On the one hand, caller methods can convert, if necessary, a value of
IEnumerable<T> type into many other collection types or use it with LINQ. On the other hand, developers are likely to be able to change the method’s implementation without the need to change the return type.
Also, returning a more general type may help in the future if you decide to change the return value to a more specific type, for example,
List<T>: if callers expect
IEnumerable<T>, they will be able to accept
List<T>, but not vice versa.
Below, JetBrains Rider suggests changing the return type of
Note that such a replacement is not always possible, though. If methods of the derived type are used on the returned object anywhere in the solution, JetBrains Rider will not issue this suggestion.
|Suboptimal code||After the quick-fix|
| || |
In some cases, however, returning a more specific type might be better. If a method is intended to work only with a specific type like an
Array, and its implementation is unlikely to be changed in the future, you can suppress this inspection for that method.