Func delegate with no return type
Asked Answered
L

7

706

All of the Func<T> delegates return a value. What are the .NET delegates that can be used with methods that return void?

Lepp answered 27/5, 2009 at 19:21 Comment(0)
U
938

All Func delegates return something; all the Action delegates return void.

Func<TResult> takes no arguments and returns TResult:

public delegate TResult Func<TResult>()

Action<T> takes one argument and does not return a value:

public delegate void Action<T>(T obj)

Action is the simplest, 'bare' delegate:

public delegate void Action()

There's also Func<TArg1, TResult> and Action<TArg1, TArg2> (and others up to 16 arguments). All of these (except for Action<T>) are new to .NET 3.5 (defined in System.Core).

Unhesitating answered 27/5, 2009 at 19:24 Comment(5)
FYI, the next version of the base class library will include Func and Action types that support more than four formal parameters. I don't recall exactly how big they get.Spirochete
Actually, it looks like they go up to 16 in 4.0.Weighbridge
1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, ... this clearly indicates that the compiler will need to be able to cope with more arguments to a function than it currently does at some point in the future!Clearcut
Actually, Tustin2121 is right, they went up to 16 parameters (plus a return type in the case of Func<,,, ... ,>) in .NET 4.0, but the last eight types of each "series" are defined in System.Core.dll, not in mscorlib.dll, so that would be the reason why michielvoo didn't see them. However, no more Funcs or Actions were added in .NET versions 4.5 and 4.5.1. Will this sequence become A170836 or A170875? Stay tuned.Nicias
I get providing a few overloads with several arguments for convenience, but a certain point, just wrap your args in an object...Arbour
L
115

... takes no arguments and has a void return type?

I believe Action is a solution to this.

Lepp answered 27/5, 2009 at 19:23 Comment(0)
V
62

All of the Func delegates take at least one parameter

That's not true. They all take at least one type argument, but that argument determines the return type.

So Func<T> accepts no parameters and returns a value. Use Action or Action<T> when you don't want to return a value.

Valdemar answered 27/5, 2009 at 19:23 Comment(0)
G
31

Try System.Func<T> and System.Action

Grunt answered 27/5, 2009 at 19:22 Comment(4)
I don't think the 0 arg and whatnot exist in .Net 2.0, though.Unrig
It's weird: Func doesn't exist at all in .Net 2.0, though Predicate and Action do.Valdemar
For .NET 2.0 use MethodInvoker delegate.Photoengrave
.NET 2 also had (or has) a delegate type Converter<TInput, TOutput> which was like the later Func<T, TResult>. It was used in the List<>.ConvertAll method which projected every element in a List<> onto another object, and placed all the "function values" in a new List<>. (Later, one would often use Linq Select for that.)Nicias
T
8

A very easy way to invoke return and non return value subroutines. is using Func and Action respectively. (see also https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx)

Try this this 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)  
    {  
        Console.WriteLine(message);  
        return null;  
    }  

    private string Display(string message1,string message2)  
    {  
        Console.WriteLine(message1);  
        Console.WriteLine(message2);  
        return null;  
    }  

    public void ObjectProcess(object param)
    {
        if (param == null)
        {
            throw new ArgumentNullException("Parameter is null or missing");
        }
        else 
        {
            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());
    }  
}
Tutuila answered 25/1, 2018 at 17:6 Comment(1)
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.Clockwise
W
1

Occasionally you will want to write a delegate for event handling, in which case you can take advantage of System.EvenHandler<T> which implicitly accepts an argument of type object in addition to the second parameter that should derive from EventArgs. EventHandlers will return void

I personally found this useful during testing for creating a one-off callback in a function body.

Willwilla answered 19/10, 2016 at 18:4 Comment(0)
S
1

... takes no arguments and has a void return type?

If you are writing for System.Windows.Forms, You can also use:

public delegate void MethodInvoker()
Sterlingsterlitamak answered 25/2, 2018 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.