Skip to content

Instantly share code, notes, and snippets.

@tany3
Created May 15, 2014 08:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tany3/2d032cbbbd9db04b976d to your computer and use it in GitHub Desktop.
Save tany3/2d032cbbbd9db04b976d to your computer and use it in GitHub Desktop.
重複処理 IEnumerableをそのまま使い回さない - Possible multiple enumeration of IEnumerable

IEnumerableをそのまま使うと、使う度に毎回変換される。遅延実行。 http://confluence.jetbrains.com/display/ReSharper/Possible+multiple+enumeration+of+IEnumerable

Assuming that GetNames() returns an IEnumerable, we are, effectively, doing extra work by enumerating this collection twice in the two foreach statements. If GetNames() results in a database query, you end up doing that query twice, while both times getting the same data.

ということで、特に複数回使う場合など適宜ToList()やToArray()しておく。

参考:LINQ クエリの概要 (C#) http://msdn.microsoft.com/ja-jp/library/bb397906.aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment