I always think of delegate as a formula, the last parameter represents the result, and the previous parameters are all unknowns.
When using it, just put the actual number into the formula.
Func<int, int, int> func = (value1, value2) => value1 + value2;
int re = func(3, 4);
Console.WriteLine(re);
Update:
TestAAA is a method, so we should pass two parameters when calling it.
Moreover, the return value of this method is an instance of RETURN_T1 type, not the required delegate.
The compiler judged that the parameter type was incorrect, so the current error occurred.
Func<Group1, Group2, RETURN_T1> func = (f1, f2) =>
{
RETURN_T1 t1 = new RETURN_T1();
return t1;
};
RETURN_T1 rETURN_T1 = TestAAA(new Group1(), new Group2());
Moreover, DefaultGroup13 is an instance field, we cannot use other instance fields or methods in its parameters, otherwise it will cause Compiler Error CS0236, so TestAAA needs to be set to the static method.
Try to modify the code to look like this:
protected ExFunction DefaultGroup13 = new ExFunction(new ExSubFunction(5, new DateTime(0), null, (f1,f2)=>TestAAA(f1,f2)));
protected static RETURN_T1 TestAAA(Group1 a1, Group2 a2)
{
RETURN_T1 t1 = new RETURN_T1();
return t1;
}
Update(5/13):
This is related to the initialization order of class members.
DefaultGroup13 and TestAAA are at the same priority of initialization. Since we can change their order at will, it may cause TestAAA not to exist when DefaultGroup13 is initialized, and we can't use things that don't exist.
If we add static to the method, the priority of this method initialization will be higher than the instance field DefaultGroup13, which can ensure that TestAAA has been initialized when DefaultGroup13 is initialized.
Initialization order of class members:
Derived static fields
Derived static constructor
Derived instance fields
Base static fields
Base static constructor
Base instance fields
Base instance constructor
Derived instance constructor
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.