什么是代表与放大器之间的差异;在C#中的行动 [英] What is the difference between Delegate & Action in C#

查看:200
本文介绍了什么是代表与放大器之间的差异;在C#中的行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我搜索我肯定会得到展示的是一个代表和行动的例子。
我已阅读书籍基本委托给的例子。

但我想知道的是他们在现实世界中使用。请不要给Hello World示例或动物类,他们都太基本

有关如


  1. 什么是它们之间的区别

  2. 当使用委托或行动

  3. 当不使用一个委托或行动

  4. 当他们可以是超必杀


解决方案

动作的一个代表。它的定义是这样的:

 公共委托无效动作();

您同样可以创建自己的委托类型是如何创建抽象方法;你写的签名,但没有实现。然后,通过采取方法的引用创建这些代表的实例。

 类节目
{
    公共静态无效FooMethod()
    {
        Console.WriteLine(叫做foo);
    }    静态无效的主要()
    {
        行动富= FooMethod; // foo的现在引用FooMethod()
        富(); //输出叫做foo
    }
}

If I searched I will definitely get examples of showing what is a Delegate and Action. I have read the basic books giving examples of delegate.

But what I want to know is their use in the real world. Please do not give Hello World example or the Animal class they are far too basic

For e.g. :

  1. What are the difference between them
  2. When to use a delegate or action
  3. When not to use a delegate or action
  4. When they can be an over kill

解决方案

Action is a Delegate. It is defined like this:

public delegate void Action();

You could create your own delegate types similarly to how you would create abstract methods; you write the signature but no implementation. You then create instances of these delegates by taking the reference of a method.

class Program
{
    public static void FooMethod()
    {
        Console.WriteLine("Called foo");
    }

    static void Main()
    {
        Action foo = FooMethod; // foo now references FooMethod()
        foo(); // outputs "Called foo"
    }
}

这篇关于什么是代表与放大器之间的差异;在C#中的行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆