Icetiger
Icetiger

Reputation: 23

C# Action, Func , Can be Nested or Chaining?

I am Novice in Delegate (in English too).

This is my sample code:

var expected_for = new int[Length];

Func<int[], Action<int>> opFactory = res => i => res[i] = arg1[i] + arg2[i];

Parallel.For(0, arg1.Length, opFactory(expected)); // working well

Enumerable.Range(0, arg1.Length).ToList().ForEach(opFactory(expected_foreach));  // working well

         for (int i = 0; i < arg1.Length; i++)
            {
                opFactory(expected_for); //No Error, but expected_for is not changed
            }

Q1. Func<int[], Action<int>> opFactory = res => i => res[i] = arg1[i] + arg2[i]; in Func, Action can be nested? This is too difficult to understand for me.

Q2. Parallel.For's third argument requires Action. Then my Func Line was Action?

Q3. How can I save value in for()?

Thank you for reading

Regards

ICE

Upvotes: 0

Views: 2208

Answers (1)

George Vovos
George Vovos

Reputation: 7618

Delegates are simpler than you think.
Let's take Func.The last argument is the result that must be returned.
So you need to specify at least one generic parameter
For examaple Func<int,string> can point do any methods that accept an int and return a string In the following example all 4 funcs do the same thing.

Q1:You can make them as complex as you want.
Q2:No Action is not the same a Func.It works because the is an overload that accepts a Func Q3:It didn't work because without the (i) you don't actually call the method.You called opFactory and ignored its result

using System;
namespace ConsoleApp1
{

    class Program
    {
        static void Main(string[] args)
        {
            ///lamda statement
            Func<int, string> func4 = i => { return i.ToString(); };

            ///lamda expression (if there is only 1 statement)
            Func<int, string> func3 = i => i.ToString();

            Func<int, string> func2 = delegate (int i) { return i.ToString(); };

            //Just point to an existing method
            Func<int, string> func1 = ToString;
        }

        static string ToString(int arg)
        {
            return arg.ToString();
        }
    }
}

And a full example

using System;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp1
{

    class Program
    {
        static void Main(string[] args)
        {
            int Length = 5;

            var arg1 = Enumerable.Range(10, Length).ToArray();
            var arg2 = Enumerable.Range(100, Length).ToArray();

            var expected = new int[Length];
            var expected_for = new int[Length];
            var expected_foreach = new int[Length];

            Func<int[], Action<int>> opFactory = res => i => res[i] = arg1[i] + arg2[i];

            Parallel.For(0, arg1.Length, opFactory(expected));

            Enumerable.Range(0, arg1.Length).ToList().ForEach(opFactory(expected_foreach));

            for (int i = 0; i < arg1.Length; i++)
            {
                opFactory(expected_for)(i);
            }

            //all 3 tables have the same data 110,112,114,116,118
            Console.ReadLine();
        }
    }
}

Upvotes: 1

Related Questions