2017-09-18 8 views
0

Je suis novice en tant que déléguée (en anglais aussi).Action C#, Func, Peut être imbriqué ou chaîné?

Ceci est mon exemple de 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]; dans Func, Action peut être imbriqué? C'est trop difficile à comprendre pour moi.

Q2. Le troisième argument de Parallel.For requiert Action. Puis ma ligne Func était Action?

Q3. Comment puis-je enregistrer la valeur dans for()?

Merci de lire

Cordialement

ICE

+0

au sujet Q3, j'ai trouvé la solution "opFactory (expected_for) (i)" – Icetiger

Répondre

1

Les délégués sont plus simples que vous pensez.
Prenons Func. Le dernier argument est le résultat qui doit être retourné.
Vous devez donc spécifier au moins un paramètre générique
Par exemple Func<int,string> peut pointer n'importe quelle méthode qui accepte un entier et renvoie une chaîne Dans l'exemple suivant, les 4 fonctions font la même chose.

Q1: Vous pouvez les rendre aussi complexes que vous le souhaitez.
Q2: Aucune action n'est identique à un Func.Il fonctionne car il s'agit d'une surcharge qui accepte un Func Q3: Cela n'a pas fonctionné car sans le (i) vous n'appelez pas réellement la méthode. Vous avez appelé opFactory et a ignoré le résultat

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

et un exemple complet

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(); 
     } 
    } 
} 
+0

réponse vraiment parfait pour moi. Merci beaucoup – Icetiger