C# 具有零参数的动作委托

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/593205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:34:50  来源:igfitidea点击:

action delegate with zero parameters

c#delegatesaction

提问by I. J. Kennedy

I see this line in many online examples of using the Action delegate:

我在许多使用 Action 委托的在线示例中看到了这一行:

public event Action MyEvent;

But when I try it in my own code, I get this error

但是当我在自己的代码中尝试时,出现此错误

Using the generic type 'System.Action' requires '1' type arguments

使用泛型类型“System.Action”需要“1”类型参数

The documentation certainly describes a form of Action without any type parameter. What am I missing?

该文档当然描述了一种没有任何类型参数的 Action 形式。我错过了什么?

采纳答案by Andrew Hare

Make sure your application is referencing System.Core.

确保您的应用程序正在引用System.Core.

Edit - also make sure you are targeting .NET 3.5 as the System.Core.dll is part of that version.

编辑 - 还要确保您的目标是 .NET 3.5,因为 System.Core.dll 是该版本的一部分。

回答by JaredPar

Expanding on Andrews answer.

扩展安德鲁斯的答案。

It's perfectly legal to use Action in a non-3.5 scenario. Simply define it yourself.

在非 3.5 场景中使用 Action 是完全合法的。只需自己定义即可。

public delegate void Action();