How you try to use delegates (constructing them with new by declaring a named type of delegation) assumes that you are using C # 1. If you really use C # 3, this is a lot easier than that.
First, your delegate type:
public delegate List<BaseNode> GetAllNodesDelegate(int k);
Already exists. It's simple:
Func<int, List<BaseNode>>
Therefore, you do not need to declare your own version.
Secondly, you should think about the fact that a delegate is like an interface with only one method, and you can "implement" it on the fly, without having to write a named class. Just write lambda or directly assign a method name.
Func<int, List<BaseNode>> getNodesFromInt; // just assign a compatible method directly getNodesFromInt = DoSomethingWithArgAndReturnList; // or bind extra arguments to an incompatible method: getNodesFromInt = arg => MakeList(arg, "anotherArgument"); // or write the whole thing specially: getNodesFromInt = arg => { var result = new List<BaseNode>(); result.Add(new BaseNode()); return result; };
The lambda has the form (arguments) => { body; } (arguments) => { body; } . Arguments are separated by a comma. If there is only one, you can omit the parentheses. If it does not accept any parameters, put a couple of empty parentheses: () . If the body has only one statement, you can omit curly braces. If this is just one expression, you can omit the curly braces and the return keyword. In the body, you can refer to almost any variables and methods from the covering area (except for ref / out parameters to the placement method).
You almost never need to use new to instantiate a delegate. And it is rarely required to declare custom delegate types. Use Func for delegates that return a value and Action for delegates that return a void .
Whenever the thing you need to pass looks like an object with one method (be it an interface or a class), then use a delegate instead, and you can avoid a big mess.
In particular, avoid defining interfaces in one way. It just means that instead of writing lambda to implement this method, you will have to declare a separate named class for each other implementation with a template:
class Impl : IOneMethod {
The lambda effectively does all this for you, eliminating such mechanical patterns from your code. You just say:
() =>
This also has the advantage that you can update variables in the scope because you can access them directly (instead of working with values copied to the fields of the class). This can sometimes be a disadvantage if you do not want to change the values.