Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

After googling it doesn't look promising, but I'm wondering if there is some way of aliasing or typedef'ing when using Action<T> or Func<in T, out TResult> in C#?

I've already seen Equivalent of typedef in c#, which says that within one compile scope you can use the using construct for some cases, but that doesn't seem to apply to Action's and Func's as far as I can tell.

The reason I want to do this is that I want an action to be used as a parameter to several functions, and if I, at some point in time, decide to change the action it's a lot of places to change both as parameter types and as variable types.

?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;
}

Are there some construct to use, which can define the MyAction as indicated in the code segment?

like image 268
holroy Avatar asked Jun 24 '15 11:06

holroy


People also ask

What is difference between typedef and macro in C?

We can have symbolic names to datatypes using typedef but not to numbers etc. Whereas with a macro, we can represent 1 as ONE, 3.14 as PI and many more. We can have a type name and a variable name as same while using typedef. Compiler differentiates both.

What is the syntax of typedef in C?

typedef <existing_name> <alias_name> In the above syntax, 'existing_name' is the name of an already existing variable while 'alias name' is another name given to the existing variable.

What is typedef and #define in C?

Typedef is a keyword in the C programming language. #define is a pre-processor and used as macro used in C programming. 2. It is a keyword used to provide an alternate name to the existing data types only. And that name can be used to initialize the variables in a program.

What is typedef in C with example?

typedef is used to define new data type names to make a program more readable to the programmer. For example: | main() | main() { | { int money; | typedef int Pounds; money = 2; | Pounds money = 2 } | } These examples are EXACTLY the same to the compiler.

Can we use #define instead of typedef?

Difference between typedef and #define: typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc.

Is using the same as typedef?

They are equivalent, from the standard (emphasis mine) (7.1. 3.2): A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name.


2 Answers

After some more searching, it seems as though delegate's comes to the rescue (see Creating delegates manually vs using Action/Func delegates and A: Custom delegate types vs Func and Action). Please comment as to why this is not a solution or possible pitfalls.

With delegates I can rewrite the first line the given code example:

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);
like image 77
holroy Avatar answered Sep 22 '22 12:09

holroy


using System;

namespace Example
{
    using MyAction = Action<int>;

    internal class Program
    {
    }

   private void DoSomething(MyAction action)
   {
   }
}
like image 21
Jegan Avatar answered Sep 22 '22 12:09

Jegan