Why GetAllPopulatedSlots return type is IEnumerable

I dont really understand why the GetAllPopulatedSlots method needs to return IEnumerable rather than an array of EquipLocations?

IEnumerable is pretty much an array of EquipLocations. We are also returning equippedItems.Keys in this case, which is a KeyCollection. Both KeyCollection and an array of EquipLocations are IEnumerable<EquipLocation> (in this case) and can therefore be returned. If we returned EquipLocation[] we would have had to create an array from the KeyCollection to return.

1 Like

@bixarrio pretty much sums this up. We don’t have a need for any of the overhead of an Array or a List.

Whenever you’re using foreach, the foreach loop is iterating over each element in an IEnumerable<T>. Because Lists, Arrays, Dictionaries, and virtually all Linq expressions are forms of IEnumerables, this is why we are able to iterate over them with foreach.

At a certain point, you’ll start experimenting with more complex IEnumerables, like Linq expressions which act very much like Database SQL over a dataset.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms