欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#委托学习示例分享

程序员文章站 2023-12-19 23:23:28
1.委托 总的来说,委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用if-else...

1.委托

总的来说,委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用if-else(switch)语句,同时使得程序具有更好的可扩展性。所以,引入委托后,编程人员可以把方法的引用封装在委托对象中,然后把委托对象传递给需要引用方法。调用委托和调用方法的方式是一模一样的,代码如下:

a.代码:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;

namespace wforms
{
    public partial class form1 : form
    {
        //定义委托
        private delegate void writetextbox(char ch);
        //声明委托
        private writetextbox writetextbox;
        public form1()
        {
            initializecomponent();
        }

        private void button1_click(object sender, eventargs e)
        {
            if (checkbox1.checked == true)
            {
                textbox1.clear();
                textbox1.refresh();
                // 实例化委托- 方法writetextbox1
                writetextbox = new writetextbox(writetextbox1);
                // 委托作为参数,在方法writetext通过委托运行writetextbox1方法
                writetext(writetextbox);

                textbox3.focus();
                textbox3.selectall();
            }
            if (checkbox2.checked == true)
            {
                textbox2.clear();
                textbox2.refresh();
                // 实例化委托 - 方法writetextbox2作为参数
                writetextbox = new writetextbox(writetextbox2);
                // 委托作为参数,在方法writetext通过委托运行writetextbox2方法
                writetext(writetextbox);
                textbox3.focus();
                textbox3.selectall();
            }
        }

        /**
         *我们通过writetext方法来向文本区写入内容,
         *它所执行的只是抽象的”写文本“操作,至于究竟向哪个文本框写入文字,
         *对于编写writetext方法的程序来说是不知道,委托writetextbox就像一个接口一样,
         *屏蔽了操作对象的差别(方法到底是想向文本区1写入文本还是像文本区2写入文本,
         *现在我方法里面不需要去关心,
         *我只需要集中在实现”书写文本”这个操作,而不必纠结操作对象的选择)。
         */
        private void writetext(writetextbox writetextbox)
        {
            string data = textbox3.text;
            for (int i = 0; i < data.length; i++)
            {
                // 使用委托 - 通过委托的不同运行不同的方法
                writetextbox(data[i]);
                //间歇延时
                datetime now = datetime.now;
                while (now.addseconds(1) > datetime.now) { }
            }
        }
        //向文本区1添加字符
        private void writetextbox1(char ch)
        {
            textbox1.appendtext(ch.tostring());
        }
        //向文本区2添加字符
        private void writetextbox2(char ch)
        {
            textbox2.appendtext(ch.tostring());
        }
    }
}
form1.cs

b.效果图:

c#委托学习示例分享

2.委托链

其实委托链就是一个委托,只是包含了多个委托而已。看完下面代码,应该可以很明白。

a.代码:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace consoleapplication1
{
    class program
    {
        // 声明一个委托类型,它的实例引用一个方法,该方法返回一个string类型
        public delegate string delegatetest();
        public static void main(string[] args)
        {
            // 用静态方法来实例化委托
            delegatetest dtstatic = new delegatetest(program.method1);

            // 用实例方法来实例化委托
            delegatetest dtinstance = new delegatetest(new program().method2);
            delegatetest dtinstance2 = new delegatetest(new program().method3);
            // 定义一个委托链对象,一开始初始化为null,就是不代表任何方法(我就是我,我不代表任何人)
            delegatetest delegatechain = null;
            delegatechain += dtstatic;
            delegatechain += dtinstance;
            delegatechain += dtinstance2;
            // environment.newline - 换行符
            console.writeline(environment.newline + dtstatic() + environment.newline);// 隐式调用委托
            console.writeline(dtstatic.invoke() + environment.newline);// 显式调用委托
            console.writeline(environment.newline + test(delegatechain));//输出字符串
            console.read();

        }
        private static string method1()
        {
            return "这是静态方法1";
        }

        private string method2()
        {
            throw new exception("抛出了一个异常");
        }

        private string method3()
        {
            return "这是实例方法3";
        }
        // 测试调用委托的方法
        private static string test(delegatetest chain)
        {
            if (chain == null)
            {
                return null;
            }

            // 用这个变量来保存输出的字符串
            stringbuilder returnstring = new stringbuilder();

            // getinvocationlist方法返回一个由delegate引用构成的数组,
            //其中每一个数组都指向链中的一个委托对象。
            delegate[] delegatearray = chain.getinvocationlist();

            // 遍历数组中的每个委托
            foreach (delegatetest t in delegatearray)
            {
                try
                {
                    //调用委托获得返回值
                    returnstring.append(t() + environment.newline);
                }
                catch (exception e)//异常
                {
                    returnstring.appendformat("异常从 {0} 方法中抛出, 异常信息为:{1}{2}", t.method.name, e.message, environment.newline);
                }
            }

            // 把结果返回给调用者
            return returnstring.tostring();
        }

    }
}
program.cs

b.效果图:

c#委托学习示例分享

上一篇:

下一篇: