Tuesday, October 6, 2009

Sample Code of Func<T,bool>

Just some sample code of passing a predicate into a method.
   1:  using System; 


   2:   


   3:  namespace Func 


   4:  { 


   5:      class Program 


   6:      { 


   7:          static void Main(string[] args) 


   8:          { 


   9:              string[] weekDays = {"Sun", "Sat", "Mon", "Tue", "Wed", "Thu", "Fri"}; 


  10:   


  11:              Display(weekDays, s => s.Length > 2 ); 


  12:              Console.ReadLine(); 


  13:          } 


  14:   


  15:          private static void Display(string[] name, Func<string, bool> func) 


  16:          { 


  17:              foreach (string s in name) 


  18:              { 


  19:                  if (func(s)) Console.WriteLine(s); 


  20:              } 


  21:          } 


  22:      } 


  23:  }


No comments:

Post a Comment