C#第八章读书笔记-委托、lambda表达式和事件

1.定义一个委托实际上是定义一个新类

2.

        private delegate string GetAString();
        static void Main(string[] args)
        {
            int x = 0;
            GetAString getAString = new GetAString(x.ToString);
            Console.WriteLine(getAString());
            Console.ReadKey();
        }
3.
        private delegate string GetAString();
        static void Main(string[] args)
        {
            int x = 0;
            // GetAString getAString = new GetAString(x.ToString);
            GetAString getAString = x.ToString;
            //这两个是一样的
            Console.WriteLine(getAString());
            Console.WriteLine(getAString.Invoke());
            Console.ReadKey();
        }
4.
 public class Mathtest
    {
        public static double Method1(double value)
        {
            return value;
        }
        public static double Method2(double value)
        {
            return value* value;
        }
    }
    class Program
    {
        delegate double DoubleOp(double x);
        static void Main(string[] args)
        {
            DoubleOp[] test =
            {
                Mathtest.Method1,
                Mathtest.Method2

            };
            foreach (var item in test)
            {
               Console.WriteLine(item(12)); 
            }
            Console.ReadKey();
        }
    }

5.


6.

  public class Mathtest
    {
        public static double Method1(double value)
        {
            return value;
        }
        public static double Method2(double value)
        {
            return value * value;
        }
    }
    class Program
    {
        //delegate double DoubleOp(double x);
        static void Main(string[] args)
        {
            Func<double, double>[] test =
            {
                Mathtest.Method1,
                Mathtest.Method2

            };
            foreach (var item in test)
            {
                Console.WriteLine(item(12));
            }
            Console.ReadKey();
        }
    }

7.BubbleSorter,一数字的冒泡排序为例,引出能给任何对象排序的例子

  class Program
    {
        class BubbleSorter
        {
            public static void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison)
            {
                bool swap = true;
                do
                {
                    swap = false;
                    for (int i = 0; i < sortArray.Count - 1; i++)
                    {
                        if (comparison(sortArray[i + 1], sortArray[i]))
                        {
                            T temp = sortArray[i];
                            sortArray[i] = sortArray[i + 1];
                            sortArray[i + 1] = temp;
                            swap = true;
                        }
                    }
                } while (swap);
            }
        }

        class employee
        {
            public string Name { get; set; }
            public decimal Salary { get; set; }

            public employee(string name, decimal salary)
            {
                Name = name;
                Salary = salary;
            }

            public override string ToString()
            {
                return this.Name + "---" + this.Salary;
            }

            public static bool CompareSalary(employee e1, employee e2)
            {
                return e1.Salary < e2.Salary;
            }
        }
        static void Main(string[] args)
        {
            employee[] employees =
            {
                new employee("wang",4000),
                new employee("zhang",5000),
                new employee("li",6000),
                new employee("zhao",1000),
            };
            BubbleSorter.Sort(employees, employee.CompareSalary);
            foreach (var item in employees)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }

8.多播委托委托可以包含多个方法调用

运行结果:

method11111
method22222
method11111

method22222

using System;

namespace ConsoleApplication1
{

    class Program
    {
        public class Mathtest
        {
            public static void Method1(double value)
            {
                Console.WriteLine("method11111");
            }

            public static void Method2(double value)
            {
                Console.WriteLine("method22222");
            }
            public static void Method3(double value, double asa)
            {
                Console.WriteLine("method333333");
            }
        }
        /// <summary>
        /// 书上采用的这种写法,这个方法还必须是静态的
        /// </summary>
        /// <param name="action"></param>
        /// <param name="number"></param>
        static void ProcessAndDisPlayNumber(Action<double> action, double number)
        {
            action(number);
        }
        //delegate double DoubleOp(double x);
        static void Main(string[] args)
        {
            //这个Action<double>是不带返回值的委托,Func<>是带返回值的委托
            Action<double> test = Mathtest.Method1;
            test += Mathtest.Method2;
            test(12);
            ProcessAndDisPlayNumber(test, 123);
            Console.ReadKey();
        }
    }


}

多播委托调用多个方法时,如果遇到抛出异常的情况,会终止迭代,导致不能完成对所有方法的调用,不过可以避免这种情况:


using System;

namespace ConsoleApplication1
{

    class Program
    {
        public class Mathtest
        {
            public static void Method1()
            {
                Console.WriteLine("one");
                throw new Exception("error in one");
            }

            public static void Method2()
            {
                Console.WriteLine("two");
            }
            public static void Method3(double value, double asa)
            {
                Console.WriteLine("three");
            }
        }
        /// <summary>
        /// 书上采用的这种写法,这个方法还必须是静态的
        /// </summary>
        /// <param name="action"></param>
        static void ProcessAndDisPlayNumber(Action action)
        {
            action();
        }
        //delegate double DoubleOp(double x);
        static void Main(string[] args)
        {
            //这个Action是不带参数的委托,Func<>是带参数和返回类型的委托
            Action test = Mathtest.Method1;
            test += Mathtest.Method2;
            test();//没加捕获异常函数
            ProcessAndDisPlayNumber(test);//没加捕获异常函数
            Console.ReadKey();
        }
    }


}

下面是如何避免这种情况:

using System;

namespace ConsoleApplication1
{

    class Program
    {
        public class Mathtest
        {
            public static void Method1()
            {
                Console.WriteLine("one");
                throw new Exception("error in one");
            }

            public static void Method2()
            {
                Console.WriteLine("two");
            }
            public static void Method3(double value, double asa)
            {
                Console.WriteLine("three");
            }
        }
        /// <summary>
        /// 书上采用的这种写法,这个方法还必须是静态的
        /// </summary>
        /// <param name="action"></param>
        static void ProcessAndDisPlayNumber(Action action)
        {
            action();
        }
        //delegate double DoubleOp(double x);
        static void Main(string[] args)
        {
            //这个Action是不带参数的委托,Func<>是带参数和返回类型的委托
            Action test = Mathtest.Method1;
            test += Mathtest.Method2;
            //自己规定调用的顺序
            Delegate[] delegates = test.GetInvocationList();
            foreach (Action item in delegates)
            {
                try
                {
                    item();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);                   
                }
            }
            test();
            ProcessAndDisPlayNumber(test);
            Console.ReadKey();
        }
    }


}

9.匿名方法



using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string> test = delegate (string str)
            {
                return str;
            };
            Console.WriteLine(test("12313"));
            Console.ReadKey();
        }
    }
}


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string> test = str =>
            {
                return str;
            };
            Console.WriteLine(test("12313"));
            Console.ReadKey();
        }
    }
}



using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string, string> test = (string str1, string str2) =>
            {
                return str1 + str2;
            };
            Console.WriteLine(test("1111", "222"));
            Console.ReadKey();
        }
    }
}
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string, string> test = (string str1, string str2) =>
            {
                return str1 + str2;
            };
            Func<string, string, string> test1 = ( str1,  str2) =>
            {
                return str1 + str2;
            };
            Console.WriteLine(test("1111", "222"));
            Console.WriteLine(test1("333", "444"));
            Console.ReadKey();
        }
    }
}

10.闭包

110

110

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 10;
            Func<int, int> test = x => { return x + 10; };
            Console.WriteLine(test(100));
            num = 100;
            Console.WriteLine(test(100));
            Console.ReadKey();
        }
    }
}


11.

using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var test = new List<int>() { 1, 2, 3 };
            var funs = new List<Func<int>>();
            foreach (var item in test)
            {
                funs.Add(() => item);
            }
            foreach (var item in funs)
            {
                Console.WriteLine(item());
            }
            Console.ReadKey();
        }
    }
}

.......待细看


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值