简体   繁体   English

FUNC <void> 作为输入参数

[英]Func<void> as input parameter

I have method in c# with some parameters: 我在C#中有一些参数的方法:

public static void DeleteSingleItemInDataGrid
    (DataGrid dataGrid, String IDcolumnName, Func<int> afterCompletionMethod_ToRun)

I want to change third parameter type to Func< void > but I can't. 我想将第三个参数类型更改为Func< void >但是我不能。 how can I do it? 我该怎么做?

In other words my question is how can pass a method(or function with void result ) as a method parameter? 换句话说, 我的问题是如何将方法(或具有空结果的函数)作为方法参数传递?

A delegate referencing a method that "returns" void is called an Action in .NET lingo, and that's what the delegate type is called as well: 引用“返回” void的方法的委托在.NET术语中称为Action ,这也称为委托类型:

So your method signature would be this: 因此,您的方法签名应为:

public static void DeleteSingleItemInDataGrid
    (DataGrid dataGrid, String IDcolumnName, Action afterCompletionMethod_ToRun)

If you need to pass the int parameter to it, and not return int , it would be this: 如果您需要将int参数传递给它,而不返回int ,那就是这样:

public static void DeleteSingleItemInDataGrid
    (DataGrid dataGrid, String IDcolumnName, Action<int> afterCompletionMethod_ToRun)

This accepts a method taking an int parameter, that does not return anything (aka "returning" void ). 这接受带有int参数的方法,该方法不返回任何值(也称为“返回” void )。

This also means that you cannot create a generic method that accepts both methods returning something and methods not returning something with just one method, but need to create an overload using Action , and one using Func<T> . 这也意味着您不能创建一个通用方法,该方法既接受返回值的方法又接受不返回仅使用一个方法的值的方法,而是需要使用Action创建一个重载,而使用Func<T>创建一个重载。

The Action delegate you can use. 您可以使用的动作委托。 It will perform given task and does not return a value. 它将执行给定的任务,并且不返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM