GetNames()
returns an IEnumerable
. So if you store that result:
IEnumerable foo = GetNames();
Then every time you enumerate foo
, the GetNames()
method is called again (not literally, I can't find a link that properly explains the details, but see IEnumerable.GetEnumerator()
).
Resharper sees this, and suggests you to store the result of enumerating GetNames()
in a local variable, for example by materializing it in a list:
IEnumerable fooEnumerated = GetNames().ToList();
This will make sure that the GetNames()
result is only enumerated once, as long as you refer to fooEnumerated
.
This does matter because you usually want to enumerate only once, for example when GetNames()
performs a (slow) database call.
Because you materialized the results in a list, it doesn't matter anymore that you enumerate fooEnumerated
twice; you'll be iterating over an in-memory list twice.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…