Return to Snippet

Revision: 65080
at October 23, 2013 15:00 by azmi


Initial Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ActionPredicateFuncExample
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string> { "Coding", "in", "C#", "Rules!" };

            Action<string> tmp = (f) => Console.WriteLine(f);
            var print = new Action<string>(tmp);

            foreach (var item in list)
                print(item);

            Predicate<string> pred = new Predicate<string>(CStringSearch);
            List<string> list2 = list.FindAll(pred);
            foreach (var item in list2)
                Console.WriteLine(item);

            Func<string, bool> isContainsN = f => f.Contains("n");
            foreach (var item in list)
            {
                if (isContainsN(item)) Console.WriteLine(item);
            }

            Console.ReadLine();
        }

        private static bool CStringSearch(string s)
        {
            return (s.Contains("C"));
        }
    }
}

Initial URL


Initial Description
Short sample code on how to create and use Actions, Predicates and Funcs

Initial Title
Actions, Predicates and Funcs

Initial Tags


Initial Language
C#