设为首页 加入收藏

TOP

【C#基础】委托那些事儿(一)(一)
2019-09-17 18:23:32 】 浏览:43
Tags:基础 委托 那些 事儿

一、简单的委托

1.1  委托的声明:

  C#当中,委托(delegate)是一种方法封装,也即委托对象可以作为一种传递方法的变量来使用。

  委托也算是一种类,与类是平级的存在。在类中写delegate对象当然是允许的,毕竟C#也允许类中类。但是一般不这样做,委托的声明最好与类声明平级。

  声明:

public  delegate    void         ActionName();

        关键字       返回类型      委托变量名(函数参数)

  方法执行,有两种方式,效果相同:

  TestHandler.Invoke(实参);

  TestHandler(实参);

 

  示例:

 public delegate void TestHandler(string str);
 
 class Program
 {
     static void Main(string[] args)
     {
         TestHandler t = delegate (string str) { Console.WriteLine($"There will show : {str}"); };
         // TestHandler t = str => Console.WriteLine($"There will show : {str}");  lambda表达式,上行的语法糖。
         t.Invoke("Hello world !");
     }
 }

 

  在说到具体如何实现委托之前,先简单说明一下Action、Func是什么。

  C#规则中,Action和Func都是方法对象(委托类型),两者的区别仅在于有无返回类型。

public delegate void Action();

public delegate TResult Func<out TResult>();

 

  形参是允许添加多个的:

Action<int, string, double> action
/*============== F12 ================*/
public delegate void Action<in T1, in T2, in T3>(T1 arg1, T2 arg2, T3 arg3);
Func<string, string, string> func
/*============== F12 ================*/
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2); //(其中,TResult参数总写在形参说明的最后)
// 注:尖括号表示里面是泛型,在声明中,需要在<>内写入具体的类型。

 

  实例化这个对象,这样写:

Action<T1, T2> action = delegate(T1 arg1, T2 arg2) { 具体实现 };  类型       变量名称  关键字   (方法入参)          {..}

  例如可以这样声明:

  Action<int, string, double> action = delegate(int a, string b, double c) { };

  Func<string, string, string> func = delegate { return "test.."; };

 

  注:入参可以省略不写,但是注意可能会有一些小陷阱,见下例[1]:

// 启动新线程,.NET2.0有4个线程构造函数:

public Thread(ParameterizedThreadStart start)

public Thread(ThreadStart start)

public Thread(ParameterizedThreadStart start, int maxStackSize)

public Thread(ThreadStart start, int maxStackSize)

// 涉及的两个委托类型是:

public delegate void ThreadStart()

public delegate void ParameterizedThreadStart(object obj)

// 创建一个新线程的尝试:

new Thread(delegate ()          {  //..});

new Thread(delegate (object o)  {  //..});

new Thread(delegate             {  //..});

// 编译器将不知道第三行delegate省却的形参到底是哪个委托,因为它既可能是ThreadStart,又可以是ParameterizedThreadStart。

 

1.2  订阅的容器 event

  首先,在此需要认同一个观点:

  属性不是字段——很多时候,属性是字段的包装器,保护字段不被滥用。而包装器永远不可能是包装的东西。

 

  知道如何写封装的方法后,接下来便是——如何去打包这些方法。即:如何把这些方法添加(或者说是整合)到某个对象上。这种“添加”,一般需要一个“盒子”,它的关键字名称叫作事件(event)。

  把方法装进事件当中,C#规定了这两个符号,

  ◆ += 表示“订阅”,-= 表示“退订”;

  ◆ 实例化后,该实例的事件成员只能出现在 += 或 -= 符号的左边。

  ◆ 只要订阅了,事件一旦执行,所有订阅的方法都会执行。

 

  姑且可以把事件当成一个方法集合(+= 看做Add, -= 看做Remove)。

  声明格式见下(关键字  delegate类型  事件变量名称  =  delegate {..}):

public event EventHandler click = delegate {};

    ( * 上行省却的方法参数,编译器将参考”方法规则” )

  以上声明方法是赋值式的,即会替换掉该事件当前订阅的所有内容,此处要注意。

 

  示例:

    class Program
    {
        static void Main(string[] args)
        {
            Test a = new Test();
            a.TestEvent += delegate() { Console.WriteLine("First Action."); };
            a.TestEvent += SecondAction;
            a.TestEvent += () => { Console.WriteLine("Third Action."); };
            a.TestAction();
Console.WriteLine(
"================"); a.TestEvent -= SecondAction; a.TestAction(); } public static void SecondAction() { Console.WriteLine("Second Action."); } } class Test { public event Action TestEvent; public void
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.NET Core 使用 HttpClient SSL .. 下一篇asp.net core系列 38 WebAPI 返回..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目