Possible multiple enumeration of IEnumerable warning using .GetType ()

I get a ReSharper warning "Possible multiple enumeration of IEnumerable" with the following code:

public void Mymethod(IEnumerable<int> entities) { var enumerator = entities.GetEnumerator(); var entityType = entities.GetType(); } 

As with many stackoverflow topics described (as well as at http://confluence.jetbrains.net/display/ReSharper/Possible+multiple+enumeration+of+IEnumerable ) ReSharper recognizes that the request is executed twice.

My question is why the GetType () operator is recognized as a request.

Any suggestion?

early.

+4
source share
2 answers

This is just uncomfortable. GetType not a virtual method; it cannot affect IEnumerable .

+3
source

To call GetTypes , entities will need to be evaluated in terms of ReSharper (he does not know if GetTypes require numbering to be evaluated, so it says "multiple enumeration is possible"). Since ReSharper sees that there are several places in this method where you have the same script, it generates this warning.

This may or may not be a problem, depending on what constitutes entities and what operation you perform on it. If it is an array in memory or you are performing an operation that does not iterate through the list, there is nothing to worry about. If you reprogram it and represent a query that will be sent to the database, it is probably useful to list it explicitly (by calling ToList or ToArray ) and acting on the result of this.

0
source

Source: https://habr.com/ru/post/1391393/


All Articles