How to use a list of Func delegate in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main() {
        var arr = new int[] { 1, 2, 3, 4, 5, 6 };

        Func<int, int> add1 = x => x + 1;
        Func<int, int> square = x => x * x;
        Func<int, int> cube = x => x * x * x;
        
        var functions = new List<Func<int, int>> {
            add1, square, cube
        };

        foreach (var fn in functions) {
            var result = arr.Select(fn);
        
            Console.WriteLine(string.Join(" ", result));
        }
    }
}




/*
run:

2 3 4 5 6 7
1 4 9 16 25 36
1 8 27 64 125 216

*/

 



answered Jul 1, 2023 by avibootz

Related questions

1 answer 107 views
1 answer 104 views
1 answer 124 views
3 answers 138 views
138 views asked Jul 1, 2023 by avibootz
1 answer 166 views
1 answer 154 views
1 answer 145 views
...