Coder Perfect

No return type for a func delegate


The Func delegates all produce a value. What are the delegates that can be used with void-returning functions in.NET?

Asked by Marcel Lamothe

Solution #1

All Func delegates return a value, but all Action delegates return a value of void.

TResult is returned by FuncTResult>, which accepts no parameters and returns TResult:

public delegate TResult Func<TResult>()

Action T> is a one-argument function that does not return a value:

public delegate void Action<T>(T obj)

The simplest, ‘bare’ delegate is action:

public delegate void Action()

There’s also ActionTArg1, TArg2> and FuncTArg1, TResult> (and others up to 16 arguments). Except for ActionT>, all of these are new in.NET 3.5. (defined in System.Core).

Answered by Jason

Solution #2

Action, in my opinion, is a viable solution.

Answered by Marcel Lamothe

Solution #3

That is not the case. They all take at least one type argument, but the return type is determined by that argument.

As a result, FuncT> takes no arguments and returns a value. When you don’t wish to return a value, use Action or ActionT>.

Answered by Joel Coehoorn

Solution #4

System.FuncT> and System.Action are two options.

Answered by JaredPar

Solution #5

Using Func and Action, respectively, is a relatively simple approach to invoke return and non-return value subroutines. (For more information, go to

Consider the following example.

using System;

public class Program
    private Func<string,string> FunctionPTR = null;  
    private Func<string,string, string> FunctionPTR1 = null;  
    private Action<object> ProcedurePTR = null; 

    private string Display(string message)  
        return null;  

    private string Display(string message1,string message2)  
        return null;  

    public void ObjectProcess(object param)
        if (param == null)
            throw new ArgumentNullException("Parameter is null or missing");
            Console.WriteLine("Object is valid");

    public void Main(string[] args)  
        FunctionPTR = Display;  
        FunctionPTR1= Display;  
        ProcedurePTR = ObjectProcess;
        FunctionPTR("Welcome to function pointer sample.");  
        FunctionPTR1("Welcome","This is function pointer sample");   
        ProcedurePTR(new object());

Answered by Aarón Ibañez Werthermänn

Post is based on