Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Func delegate with no return type

Tags:

c#

.net

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

like image 940
Marcel Lamothe Avatar asked May 27 '09 19:05

Marcel Lamothe


People also ask

Can delegates return type?

delegate: It is the keyword which is used to define the delegate. return_type: It is the type of value returned by the methods which the delegate will be going to call. It can be void. A method must have the same return type as the delegate.

What will happen if a delegate has a non void return type?

When the return type is not void as above in my case it is int. Methods with Int return types are added to the delegate instance and will be executed as per the addition sequence but the variable that is holding the return type value will have the value return from the method that is executed at the end.

Which of the following type of delegate can not return a value?

Anonymous function converted to a void returning delegate cannot return a value.

Does the function delegate receive only one value?

string GetMessage() { return "Hello there!"; } Func<string> sayHello = GetMessage; Console. WriteLine(sayHello()); In the example, we use the Func delegate which has no parameters and returns a single value. This is the function to which we refer with the help of the Func delegate.


2 Answers

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).

like image 103
Jason Avatar answered Sep 21 '22 15:09

Jason


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

I believe Action is a solution to this.

like image 32
Marcel Lamothe Avatar answered Sep 19 '22 15:09

Marcel Lamothe