Get all returned strings in methods

I have a question about delegation in this code, I am adding three methods for delegation. The string will return. In line

string delOut = del ("Beer");

give my valuable delegate delOut this "Length: 4"

How can I collect all the rows returned by methods in delegates?

public class NaForum { public delegate string MyDelegate(string s); public void TestDel() { MyDelegate del = s => s.ToLower(); del += s => s.ToUpper(); del += s => string.Format("Length : {0}", s.Length); string delOut = del("Beer"); Console.WriteLine(delOut); } } 

Thanks for any answers.

+4
source share
2 answers

You need to use Delegate.GetInvocationList :

 var results = new List<string>(); foreach (MyDelegate f in del.GetInvocationList()) { results.Add(f("Beer")); } 

Now results contains all the return values.

+9
source

See C #: Creating a multicast delegate with a boolean return type : you need to multicast yourself to get individual return values.

+1
source

Source: https://habr.com/ru/post/1486821/


All Articles