什么是代表与放大器之间的差异;在C#中的行动 [英] What is the difference between Delegate & Action in C#
问题描述
如果我搜索我肯定会得到展示的是一个代表和行动的例子。
我已阅读书籍基本委托给的例子。
但我想知道的是他们在现实世界中使用。请不要给Hello World示例或动物类,他们都太基本
有关如
- 什么是它们之间的区别
- 当使用委托或行动
- 当不使用一个委托或行动
- 当他们可以是超必杀
动作
的是的一个代表
。它的定义是这样的:
公共委托无效动作();
您同样可以创建自己的委托类型是如何创建抽象方法;你写的签名,但没有实现。然后,通过采取方法的引用创建这些代表的实例。
类节目
{
公共静态无效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. :
- What are the difference between them
- When to use a delegate or action
- When not to use a delegate or action
- 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屋!