0
0

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.

linq > 独自のWhereの定義 > static IEnumerable<int> Where(...

Last updated at Posted at 2015-07-18

https://www.youtube.com/watch?v=FupRSJe-cc0&list=PL90AF0EFFEF782D27&index=6
の6:00あたりで紹介されている。

独自の.Whereを実装できるようだ。

sample.cs
using System;
using System.Linq;

static class MainClass
{
    static IEnumerable<int> Where(
        this int[] ints, Func<int, bool> gauntlet)
    {
        Console.WriteLine("My Where()");
        foreach(int i in ints)
            if (gauntlet(i))
                yield return i;
    }

    static void Main()
    {
    
        int[] numbers = new [] {3,1,4,1,5,9,2};
        var result = 
            numbers
            .Where(n => n < 3)
            .Select(n => n);
     
        foreach(int idx in result)
            Console.WriteLine(idx);
    }
}
結果
My Where()
1
1
2

Enumerable.Where()と記載の場合は独自の実装が使われない、というようなことを言っていた(多分)。

yield return i;の理解はまだ。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?