C# Func<Type1, Type2, string> how to use it.

Markus Freitag 3,791 Reputation points
2021-05-11T15:52:44.983+00:00

Hello,

I have an external function and would like to use it.

// External:   
public CountedPanelGenerator(int count, Func<Generator, Panel, Program> programChanger = null, Func<string> externalCodeGenerator = null)  
             
        {  
            GlobalCount = Count = count;  
        }  
  
  
  
// Variant 1  
protected QueuedPanelGenerator DefaultPanelGenerator2 = new XXX(new CountedPanelGenerator(5, new DateTime(0), null, (f1, f2) =>      
        {  
  
            Program t1 = new Program();  
            return t1;  
        }  
        ));  
  
// Variant 2  
        protected QueuedPanelGenerator DefaultPanelGenerator3 = new XXX(new CountedPanelGenerator(5, new DateTime(0), null,   ????? with new ????  );  
  
        protected ProgramOrTask TestAAA(Generator a1, Panel a2)  
        {  
  
            Program t1 = new Program();  
            return t1;  
        }  

How can I simply create a function from it? With TAB is not work.
How do you experts do it?
95580-external-func-how-to-use-it-04.png

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-05-12T03:22:34.017+00:00

    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);  
    

    Func<T1,T2,TResult> Delegate

    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-05-11T23:11:37.16+00:00

    Knowing the expectations of the parameter I simply hand code the Func e.g. string is passed and returns an int.

    using System;  
      
    namespace ConsoleNetCoreApp1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Console.WriteLine(CodeSample.Execute(DummyMethod));  
                Console.ReadLine();  
            }  
            static int DummyMethod(string input) => input == "Karen" ? 0 : 1;  
        }  
        public class CodeSample  
        {  
            public static bool Execute(Func<string, int> sender)  
            {  
                return sender("Karen") == 0;  
            }  
        }  
    }  
      
    

    95743-func.png

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.