1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

csharp / LINQ > WhereやSelectなどのメソッドは、1要素ずつ処理されます

Last updated at Posted at 2015-10-24

引用: 即効入門 C#プログラミング すぐに現場で使える知識 by 中 博俊さんら
7.3 LINQ

...WhereやSelectなどのメソッドは、1要素ずつ処理されます。

test.cs
using System;
using System.Linq;

class Program
{
  static int GetValue()
  {
    System.Threading.Thread.Sleep(100);
    return 0;
  }

  static void Main() 
  {
    var q = Enumerable.Range(0, 10).Select(x => GetValue());

    // ...

    foreach (var x in q.ToArray()) // 1秒フリーズ
    {
       Console.WriteLine(DateTime.Now.Millisecond);
    }
  }
}

qをvarで代入した時には100ミリ秒のSleepは起きず、.ToArray()でxに入れる段階でSleepが起きるようだ。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?