Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do it with Predicate and Action stored in a Dictionary. I am suggesting Action here since the code snippet doesn't seems to return anything</p> <pre><code>public class IterationExample { private readonly Dictionary&lt;bool, Action&gt; dictionary; public IterationExample() { dictionary = new Dictionary&lt;bool, Action&gt; { { true, CollectionOneIterator }, { false, CollectionTwoIterator } }; } public void PublicMethod() { dictionary[condition](); } private void CollectionOneIterator() { foreach (var loopVariable in Type1Collection) { //Your code here } } private void CollectionTwoIterator() { foreach (var loopVariable in Type2Collection) { //Your code here } } } </code></pre> <p>With this way the readbility and testability of your code improves and also avoids long methods.</p> <p>Edit:</p> <pre><code>public class Entity { public IList&lt;string&gt; Type1Collection { get; set; } public IList&lt;string&gt; Type2Collection { get; set; } } public class ConsumingClass { public void Example() { var entity = new Entity(); entity.PublicMethod(); } } public static class IterationExample { private static readonly Dictionary&lt;bool, Action&lt;Entity&gt;&gt; dictionary; static IterationExample() { dictionary = new Dictionary&lt;bool, Action&lt;Entity&gt;&gt; { { true, CollectionOneIterator }, { false, CollectionTwoIterator } }; } public static void PublicMethod(this Entity entity) { dictionary[condition](); } private static void CollectionOneIterator(Entity entity) { foreach (var loopVariable in entity.Type1Collection) { //Your code here } } private static void CollectionTwoIterator(Entity entity) { foreach (var loopVariable in entity.Type2Collection) { //Your code here } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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