I have a method
Append(string text, string color, SomeOtherParamsWeLlPretendDoNotExist){}
I'd like for Append to return a reference to itself, to be able to call it like:
Append("Title","red")(" - more txt", "black")("end","green");
I'm new to C# and this exercise is more of an excuse to grasp how delegates work and what they can do.
As Delegates are strongly typed the first problem i faced was the recursive definition:
//it needs to be something like
public Func<string, string, Func<string, string, Func...>>> Append(..){..}
//i cannot use the code below to define a recursive type because Func is sealed
//on the line of the pattern found [here](https://stackoverflow.com/questions/647533/recursive-generic-types)
class MyDel: Func<string, string, MyDel>{ }
I tried keeping it simpler by defining the method as
public Func<int, Delegate> Append(string text, string color){
Console.WriteLine("{0}:{1},text,color);
return Append;
}
And i wasn't expecting it to work, but it did to some extent:
Append("first","red"); //console output: 'first:red'
Append("first","red")(" second","blue"); //console output: 'first:red second:blue'
Append("first","red")(" second","blue")("third","gray"); //compiler error: "Method name expected at (third call's first parenthesis)"
What is going on in that last case? Can i implement this behaviour at all, and how can i accomplish it?