How to declare a Func delegate that returns a Func delegate of the same type?

I would like to write a method that does some work and finally returns another method with the same signature as the original method. The idea is to process a stream of bytes depending on the previous byte value sequentially, without going into recursion. Calling it like this:

MyDelegate executeMethod = handleFirstByte //What form should be MyDelegate? foreach (Byte myByte in Bytes) { executeMethod = executeMethod(myByte); //does stuff on byte and returns the method to handle the following byte } 

To pass a method, I want to assign them to a Func delegate. But I ran into the problem that this leads to a recursive declaration without completion ...

 Func<byte, Func<byte, <Func<byte, etc... >>> 

I somehow got lost here. How can I get around this?

+12
c # delegates func
Jan 16 '15 at 17:17
source share
1 answer

You can simply declare a delegate type when Func<...> predefined delegates are not sufficient:

 public delegate RecursiveFunc RecursiveFunc(byte input); 

And if you need it, you can also use generics:

 public delegate RecursiveFunc<T> RecursiveFunc<T>(T input); 
+10
Jan 16 '15 at 17:22
source share