Equivalent typedef in C # for Action <> and / or Func <>

After searching on Google, this does not look promising, but I wonder if there is a way to smooth or typedef'ing when using Action<T> or Func<in T, out TResult> in C #?

I have already seen the equivalent of typedef in C # , which says that within one compilation area you can use the using construct for some cases, but it doesn, as far as I can tell, it seems to be applicable for Action and Func .

The reason I want to do this is because I want some action to be used as a parameter for several functions, and if at some point I decided to change the action, there are a lot of places to change as as types parameters and how types of variables.

 ?typedef? MyAction Action<int, int>; public static SomeFunc(WindowClass window, int number, MyAction callbackAction) { ... SomeOtherFunc(callbackAction); ... } // In another file/class/... private MyAction theCallback; public static SomeOtherFunc(MyAction callbackAction) { theCallback = callbackAction; } 

Is there any construction that can define MyAction , as indicated in the code segment?

+7
source share
2 answers

After a few more searches, the delegate seems to come to the rescue (see Creating delegates manually using the Action / Func and A delegates: Custom delegate types vs Func and Action ). Please comment on why this is not a solution or possible pitfalls.

With delegates, I can rewrite the first line of this sample code:

 public delegate void MyAction(int aNumber, int anotherNumber); // Keep the rest of the code example // To call one can still use anonymous actions/func/... SomeFunc(myWindow, 109, (int a, int b) => Console.Writeline); 
+6
source
 using System; namespace Example { using MyAction = Action<int>; internal class Program { } private void DoSomething(MyAction action) { } } 
+4
source

All Articles