BradleyUffner

Dynamic Filters

Sep 11th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FilterTest
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var words = new[] {"apple", "cat", "banana"};
  14.             var filters = new List<Func<string, bool>>();
  15.  
  16.             filters.Add(WordContainsA);
  17.             filters.Add(WordWordHasAtLeast5Letters);
  18.  
  19.             var result = words.Where(word => filters.All(filter => filter(word)));
  20.             foreach (var word in result)
  21.             {
  22.                 Console.WriteLine(word);
  23.             }
  24.  
  25.             Console.ReadLine();
  26.         }
  27.  
  28.         public static bool WordContainsA(string word)
  29.         {
  30.             return word.Contains("a");
  31.         }
  32.  
  33.         public static bool WordWordHasAtLeast5Letters(string word)
  34.         {
  35.             return word.Length >= 5;
  36.         }
  37.  
  38.         public static bool WordEndsWithA(string word)
  39.         {
  40.             return word.EndsWith("a");
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment