How to use the Func delegate in C#

3 Answers

0 votes
using System;

class Program
{
    static string AMessage() {
        return "May the Force be with you";
    }
    
    static void Main() {
        Func<string> say = AMessage;
        
        Console.WriteLine(say());
    }
}



/*
run:

May the Force be with you

*/

 



answered Jul 1, 2023 by avibootz
0 votes
using System;

class Program
{
    static int Sum(int x, int y) {
        return x + y;
    }
    
    static void Main() {
        Func<int, int, int> add = Sum;

        int result = add(5, 5);

        Console.WriteLine(result); 
    }
}



/*
run:

10

*/

 



answered Jul 1, 2023 by avibootz
0 votes
using System;

class Program
{
    static int Sum(int x, int y, int z) {
        return x + y + z;
    }
    
    static void Main() {
        Func<int, int, int, int> add = Sum;

        int result = add(5, 5, 5);

        Console.WriteLine(result); 
    }
}



/*
run:

15

*/

 



answered Jul 1, 2023 by avibootz

Related questions

1 answer 111 views
111 views asked Jul 1, 2023 by avibootz
1 answer 103 views
1 answer 124 views
1 answer 107 views
1 answer 166 views
1 answer 145 views
3 answers 189 views
189 views asked Jul 2, 2023 by avibootz
...