委托

参考资料:

分分钟用上C#中的委托和事件

浅谈C#中常见的委托

C#综合揭秘---深入分析委托和事件

简单的委托:

class Program
    {
        delegate void MyDelegate(string message);

        public class Example
        {
            public void Method(string message)
            {
                MessageBox.Show(message);
            }
        }

        static void Main(string[] args)
        {
            Example example=new Example();
            MyDelegate myDelegate=new MyDelegate(example.Method);
            myDelegate("Hello World");
            Console.ReadKey();
        }
    }
View Code

带返回值得委托:

class Program
    {
        delegate string MyDelegate(string message);

        public class Example
        {
            public string Method(string name)
            {
                return "Hello " + name;
            }
        }

        static void Main(string[] args)
        {
            Example example=new Example();
            //绑定委托方法
            MyDelegate myDelegate=new MyDelegate(example.Method);
            //调用委托,获取返回值
            string message = myDelegate("Leslie");
            Console.WriteLine(message);
            Console.ReadKey();
        }
    }
View Code

 

Func委托  

#region Func委托
            
            ///Func<TResult>的用法
            ///这里TResult代表函数的返回值类型
            ///只能代理返回值为TResult类型的无参函数
            Func<string> func = delegate()
            {
                return "我是Func<TResult>委托出来的结果";
            };
            Console.WriteLine(func());
            Console.ReadKey();

            ///Func<T,TResult>的用法
            ///这里的T为代理的函数的传入类型,TResult代表函数的返回值类型
            ///只能代理参数为T类型,返回值为TResult类型的函数
            Func<string, string> funcOne = delegate(string s)
            {
                return s.ToUpper();
            };
            Console.WriteLine(funcOne("我是Func<T,TResult>委托出来的结果"));
            Console.ReadKey();

            ///Func<T1,T2,TResult>的用法
            ///这里T1,T2为代理的函数的传入类型,TResult代表函数的返回值类型
            ///只能代理参数为T1,T2类型,返回值为TResult类型的函数
            Func<string, string, string> funcTwo = delegate(string value1, string value2)
            {
                return value1 + " " + value2;
            };
            Console.WriteLine(funcTwo("我是", "Func<T1,T2,TResult>委托出来的结果"));
            Console.ReadKey();

            #endregion
View Code

Action委托

      #region Action的用法
            ///Action<T>的用法
            ///这里的T为代理函数的传入类型,无返回值
            Action<string[]> action = delegate(string[] x)
            {
                var result = from p in x
                             where p.Contains("s")
                             select p;
                foreach (string s in result.ToList())
                {
                    Console.WriteLine(s);
                }
            };
            string[] str={ "charlies","nancy","alex","jimmy","selina"};
            action(str);
            Console.ReadKey();

            #endregion
View Code

Predicate委托

#region Predicate
          ///bool Predicate<T>的用法
            ///输入一个T类型的参数,返回值为bool类型
            Predicate<string[]> predicate = delegate(string[] x)
            {
                var result = from p in x
                             where p.Contains("s")
                             select p;
                if (result.ToList().Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            };
            string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" };
            if (predicate(_value))
            {
                Console.WriteLine("They contain.");
            }
            else
            {
                Console.WriteLine("They don't contain.");
            }
            Console.ReadKey();

            #endregion
View Code

总结:

Func可以接受0个至4个传入参数,必须具有返回值

Action可以接受0个至4个传入参数,无返回值

Predicate只能接受一个传入参数,返回值为bool类型
posted @ 2017-06-26 10:08  Dukezhou  阅读(80)  评论(0)    收藏  举报