question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked MarkusFreitag-0088 commented

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

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


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
1 Vote"
TimonYang-MSFT answered MarkusFreitag-0088 commented

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.

· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
my problem

 // Variant 1 works well!
 protected ExFunction DefaultGroup12 = new ExFunction(new ExSubFunction(5, new DateTime(0), null, (f1, f2) =>    
 {
     DateTime test1 = f1.Now;
     int test2 = f2.Number;
     RETURN_T1 t1 = new RETURN_T1();
     return t1;
 }
 ));
    
    
 // Variant 2 works not!
 protected ExFunction DefaultGroup13 = new ExFunction(new ExSubFunction(5, new DateTime(0), null, TestAAA));
    
 protected RETURN_T1 TestAAA(Group1 a1, Group2 a2)
 {
     RETURN_T1 t1 = new RETURN_T1();
     return t1;
 }

How can I create the function automatically?
By pressing TAB, other key combination?
Can you show me variant 2?


0 Votes 0 ·

@MarkusFreitag-0088
I revised the answer, please check it.

1 Vote 1 ·

95929-both.png



static was missing.
I'm not sure variant 1 works? No compilier error.

Can I use both?

0 Votes 0 ·
both.png (61.0 KiB)
Show more comments
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered MarkusFreitag-0088 commented

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



func.png (43.6 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks!
Can you show me please the variant 2

Thanks again! What is wrong?

I want to call TestAAA is a separate function.

95941-new-function.png


0 Votes 0 ·
new-function.png (38.7 KiB)

Thanks for your help! Can you say something, why static?

0 Votes 0 ·