How to use the Func delegate with Linq in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main() {
        Func<string, bool> Has4Letters = str => str.Length == 4;

        string[] words = 
        { 
            "play", "sing", "sci", "fi", "game", "pro", "dev" , "five", 
            "moon", "c#", "net", "cool", "programming"
        };
        
        IEnumerable<string> FourLetterWords = words.Where(Has4Letters);
        
        foreach (var word in FourLetterWords) {
            Console.WriteLine(word);
        }
    }
}




/*
run:

play
sing
game
five
moon
cool

*/

 



answered Jul 1, 2023 by avibootz

Related questions

1 answer 145 views
1 answer 124 views
1 answer 112 views
112 views asked Jul 1, 2023 by avibootz
3 answers 138 views
138 views asked Jul 1, 2023 by avibootz
1 answer 107 views
1 answer 166 views
3 answers 189 views
189 views asked Jul 2, 2023 by avibootz
...