// This below is a custom delegate
        // Creating a delegate, which will hold the pointer address
        // of all the methods that we wish to call.
        // public delegate void PhotoFilterHander(Photo photo);

        // Below we pass the delegate into our method as a param.
        public void Process(Photo photo, Action <Photo> photoFilterHander)
        {
            var filter = new PhotoFilter();

            photoFilterHander(photo); // Call the delegate with the photo.

            // Above the delegate is now incharge of handling all the photo filters to be applied.
        }
示例#2
0
        private static void Main(string[] args)
        {
            var            photoProcessor = new PhotoProcessor();
            var            filter         = new PhotoFilter();
            Action <Photo> filterHandler  = filter.ApplyBrightness;

            //PhotoProcessor.PhotoFilterHandler filterHandler = filter.ApplyBrightness;
            filterHandler += filter.ApplyContrast;
            filterHandler += RemoveRedEye;
            photoProcessor.Processor("ABC", filterHandler);
        }
示例#3
0
        public static void Main(string[] args)
        {
            // A new photo
            var photo = new Photo();

            // A photo filter to apply
            var filters = new PhotoFilter();

            // Create an instance of our photo processor class
            var processor = new PhotoProcessor();

            // A new delegate
            Action <Photo> handler = filters.GreyScale;

            // Add another filter into our process
            handler += filters.AutoFilter;

            // Adding our new red eye reduction filter
            handler += ApplyRedEyeReduction; // Note the lack of Filters. prefix

            processor.Process(photo, handler);
        }
示例#4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //Old way
            var del  = new MyDelegate(CircleArea);
            var area = del(5);

            //Using Anonymous method and lambda expression
            MyDelegate del2  = r => 3.14 * r * r;
            var        area2 = del2(4);
            //Using Func since parameter and return value is present
            Func <double, double> del3 = r => 3.14 * r * r;
            var area3 = del3(4);

            //Action used when there is no return type
            Action <string> del4 = x => Console.WriteLine(x);

            //Predicate is used for checking purpose
            Predicate <string> checkLengthGreaterThan5 = x => x.Length > 5;
            var isle = checkLengthGreaterThan5("Ashin");

            ClientClass1 clientClass1 = new ClientClass1();

            clientClass1.CallProcess();

            #region PhotoProcessing example
            var            photoProcessor = new PhotoProcessor();
            var            photoFilter    = new PhotoFilter();
            Action <Photo> handler        = photoFilter.ApplyBlackAndWhite;
            handler += photoFilter.ApplyForestFilter;
            photoProcessor.Process("url", handler);
            Console.ReadLine();



            #endregion
        }