Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you name the parameters in a Func<T> type?

I have a "dispatch map" defined as such:

private Dictionary<string, Func<DynamicEntity, DynamicEntity, IEnumerable<DynamicEntity>, string>> _messageProcessing; 

This allows me to dispatch to different methods easily depending on the name of the DynamicEntity instance.

To avoid being hated by everyone who maintains the code forever more, is there any way of naming the parameters in the Func to give some clue to which DynamicEntity is which?

like image 971
mavnn Avatar asked Jun 07 '11 08:06

mavnn


People also ask

What is func t TResult?

Func<T, TResult> defines a function that accepts one parameter (of type T) and returns an object (of type TResult).

What is Func <> C#?

Func is a delegate that points to a method that accepts one or more arguments and returns a value. Action is a delegate that points to a method which in turn accepts one or more arguments but returns no value. In other words, you should use Action when your delegate points to a method that returns void.

Do parameter names and argument names have to be the same?

The caller's arguments passed to the function's parameters do not have to have the same names.

What is the difference between Func and Action delegate?

An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type. It can contain minimum 1 and maximum of 16 input parameters and does not contain any output parameter.


1 Answers

You can't do it with the built-in Func types, but it's easy enough to create your own custom delegate type and use it in a similar way:

_messageProcessing.Add("input", (x, y, z) => "output"); _messageProcessing.Add("another", (x, y, z) => "example");  // ...  delegate string DispatchFunc(DynamicEntity first, DynamicEntity second,                              IEnumerable<DynamicEntity> collection);  Dictionary<string, DispatchFunc> _messageProcessing; 
like image 109
LukeH Avatar answered Nov 12 '22 17:11

LukeH