C# 委托的”四步走“

看了一本《深入了解C#》感觉很不错,对于委托的讲解,给大家摘录了下来!

1.什么是委托

  我的拙见:委托就是将方法作为参数,进行传递的

  书中的记载:将某种行为“包含”在一个对象中,这个对象可以像其他任何对象那样使用,在该对象中可以齿形对象的行为。可以选择将委托类型看做只定义一个方法的接口,将委托示例看做实现了那个接口的一个对象(感觉这些说发有点空洞)。

2.简单委托的构成

 为了让委托做某事,必须满足4个条件

 (1)声明委托类型(2)必须有一个方法包含要执行的的代码(3)必须创建一个委托实例(4)必须调用(invoke)委托实例

下面通过一个例子解释

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace weituo
{
    class Program
    {
        static void Main(string[] args)
        {
            //(2)第二步:必须创建一个委托实例
            ProcessDelegate process;
            Console.WriteLine("请输入用逗号分隔的两个数字:");
            string input = Console.ReadLine();
            int commaPos = input.IndexOf(',');
            double param1 = Convert.ToDouble(input.Substring(0, commaPos));
            double param2 = Convert.ToDouble(input.Substring(commaPos + 1,input.Length - commaPos -1));
 
            Console.WriteLine("输入M乘法D除法");
            input =Console.ReadLine();
 
            // 初始化委托变量
            if(input =="M")
                   //(4)必须调用(invoke)委托实例
                process = new ProcessDelegate(Multiply);
                //注释:此处也可以写process = Multiply
            else
                     //(4)必须调用(invoke)委托实例
                process = new ProcessDelegate(Divide);
 
            // 使用委托调用函数
            double result = process(param1,param2);
            Console.WriteLine("结果:{0}",result);
            Console.ReadKey();
 
        }
 
        //(1)第一步:声明委托类型
        delegate double ProcessDelegate(double param1,double param2);
       //(2)第二步:必须有一个方法包含要执行的的代码(下面是两个方法)
        static double Multiply(double param1, double param2)
        {
            return param1 * param2;
        }
 
        static double Divide(double param1, double param2)
        {
            return param1 / param2;
        }
 
    }
}

  实例二:

 

下面一个例子,自己表一下步骤,如果能找清楚,就应该大致明白了

  

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

namespace DemoConsole {
    //声明一个委托
    delegate string ConvertMethod(string inString);
    delegate void  SayHello(string sayString);
    public class Program {
        static void Main(string[] args) {
            #region MyRegion
            //string[] names = { "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" };
            //Random random = new Random(DateTime.Now.Millisecond);
            //string name = names.ElementAt(random.Next(0, names.Length));
            ////Console.WriteLine(name);
            ////Console.ReadKey();
            //Dictionary<int, string> dic = new Dictionary<int, string>();
            //dic.Add(1, "AAA");
            //dic.Add(2, "BBBB");
            //dic.Add(3, "CCCC");
            //bool t = dic.All(x => x.Key > 0);
            //bool df = dic.Any(x => x.Key > 0);
            //dic.Any(); 
            #endregion
            ConvertMethod convertMeth;
            string name = "Dakota";
            convertMeth = UppercaseString;
            Console.WriteLine(convertMeth(name));
            Person p1 = new Person();
            Person p2 = new Person();
            SayHello sp1, sp2;
            sp1 = p1.Say;
            sp2 = p2.Say;
            sp1("AAAA");
            sp2.Invoke("BBBBBB");
            Console.ReadKey();

        }
        private static string UppercaseString(string inputString) {
            return inputString.ToUpper();
        }
       
    }
    public class Person {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public void Say(string say) {
            Console.WriteLine(this.Name+"__"+say);
        }
    }
}

  

posted @ 2014-09-22 14:35  ICupid  阅读(316)  评论(0编辑  收藏  举报