delegate details

The runtime supports reference types called delegates that serve a purpose similar to that of function pointers in C++. In another word, A delegate is a reference type which is similar to function pointer. It can represent both static and instance methods. And delegates are secure, verifiable, and type safe.

 

Since I’ve learnt function pointers, I’d like to express the delegates in the way what I think. By this way, we can define a method whose parameter is also a method, or exactly a method name. This makes expansibility to the code. A delegate looks like a class. In fact, it will be compiled to a class by the compiler. Yep, delegate is a type.

 

using System;

using System.Collections.Generic;

using System.Text;

namespace myDelegate

{

    public delegate void Move(string action);

    class Test

    {

        private static void Bird(string action)

        {

            Console.WriteLine("Bird can " + action);

        }

        private static void Duck(string action)

        {

            Console.WriteLine("Duck can " + action);

        }

        private static void Dog(string action)

        {

            Console.WriteLine("Dog can " + action);

        }

        private static void moveTest(string action, Move Acter)

        {

            Acter(action);

        }

        static void Main(string[] args)

        {

            moveTest("fly", Bird);

            moveTest("swim", Duck);

            moveTest("run", Dog);

            Console.ReadLine();

        }

    }

}

There is another choice. We can avoid using moveTest() and call the methods of Bird(),Duck() and Dog() by delegate.

        

static void Main(string[] args)

        {

            Move m;

            m = Bird;   //eluvate

            m += Duck;  //syntax banding

            m += Dog;   //syntax banding

            m("eat");

            Console.ReadLine();

    } 

    

Pay attention here. The first”=”is for eluvation, and the next”+=”is a syntax of banding. If “+=”takes the place of the first “=”,errors will happen during the compiling time. Is there any way to avoid the danger? Of course,with a little modification, the code will be all right in any case. We code like this:

          

Move n = new Move(Bird);

            n += Duck;

            n("shout");

// or like this:

           Move d = new Move();

             d +=Bird;             //syntax banding here this time

d += Duck;             //banding too

            d("breathe");

 

So a delegate is able to have several values or methods.At last, I’ll talk about the accessibility of the delegates. Usually, we set the attribute of the delegates as private. In this condition,we can easily keep them from been changed by the customer side. Namely, we would better not use public to declare a delegate.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值