Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay, a few suggestions:</p> <ul> <li>Don't call your delegate <code>del</code>. In this case, I'd use <code>Func&lt;WhatHappened&gt;</code> - but if you <em>do</em> want to declare your own delegate type, give it a more descriptive name, and obey the .NET naming conventions.</li> <li><p>Instead of using anonymous methods to add to <code>CheckValues</code>, you can just use:</p> <pre><code>CheckValues.Add(method1); CheckValues.Add(method2); </code></pre> <p>The compiler will convert the method groups into delegates.</p></li> <li><p>I'd recommend not using Pascal case for a local variable name to start with.</p></li> <li>Your collection initializer for <code>returnValues</code> isn't really doing anything for you - just call the <code>List&lt;T&gt;</code> constructor as normal, or use my code below which doesn't require a local variable to start with.</li> <li>If your list <em>really</em> only has two delegates in it, I'd just call them separately. It's a lot simpler.</li> <li><p>Otherwise you can indeed use LINQ as Jared suggests, but I'd do it slightly differently:</p> <pre><code>return CheckValues.Select(x =&gt; x()) .Where(wh =&gt; wh != WhatHappened.Nothing) .ToList(); </code></pre></li> </ul> <p>EDIT: As suggested, here's the full example. It's not quite the same as Denis's though... I've made a couple of changes :)</p> <pre><code>public static List&lt;WhatHappened&gt; DoStuff() { var functions = new List&lt;Func&lt;WhatHappened&gt;&gt; { Method1, Method2 }; return functions.Select(function =&gt; function()) .Where(result =&gt; result != WhatHappened.Nothing) .ToList(); } </code></pre> <p>(I'm assuming that <code>method1</code> and <code>method2</code> have been renamed to fit the naming convention. Of course in real life I'm sure they'd have more useful names anyway...)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload