Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to create a method that returns one of two possible types?
    primarykey
    data
    text
    <p>I have 2 data structures: <code>Dictionary&lt;string, string&gt;</code> and <code>Multimap&lt;string, string&gt;</code>. Multimap is really just a Dictionary under the hood. I took must of the code from <a href="https://stackoverflow.com/questions/380595/multimap-in-net">this question</a>. Here's the class definition:</p> <pre><code>public class Multimap&lt;TKey, TValue&gt; : Dictionary&lt;TKey, HashSet&lt;TValue&gt;&gt; { ... } </code></pre> <p>Both data structures have a <code>.Add(TKey key, TValue value)</code> method.</p> <p>I have a class that is responsible for populating these maps from certain files. I currently have the following two methods:</p> <pre><code> public Dictionary&lt;string, string&gt; PopulateDictionary(...) { Dictionary&lt;string, string&gt; returnDictionary = new Dictionary&lt;string, string&gt;(); ... foreach (...) { ... returnDictionary.Add(key, value); } return returnDictionary; } public Multimap&lt;string, string&gt; PopulateMultimap(...) { Multimap&lt;string, string&gt; returnMultimap = new Multimap&lt;string, string&gt;(); ... foreach (...) { ... returnMultimap.Add(key, value); } return returnMultimap; } </code></pre> <p>As you can see, they're exactly the same, both around 25 lines long, and the only difference is their return type. What I am looking to do is condense this into one method. My first attempt was to have the method</p> <pre><code>public Dictionary&lt;string, object&gt; PopulateGenericDictionary(...) { ... } </code></pre> <p>Where <code>object</code> was either <code>string</code> or <code>HashSet&lt;string&gt;</code>. But I didn't have much luck casting from <code>Dictionary&lt;string, object&gt;</code> to <code>Multimap&lt;string, string&gt;</code>.</p> <p>Extracting the logic out of the methods is an option, but it's not great. Because of the foreach loops, there's always going to be some logic inside the two methods. You do end up with methods that are twice as small, but there's still two identical methods, which doesn't truly solve the problem.</p> <p>This would be my ideal method structure:</p> <pre><code>public Dictionary&lt;string, string&gt; PopulateDictionary(...) { return MethodThatDoesAllTheLogic(...); } public Multimap&lt;string, string&gt; PopulateMultimap(...) { return MethodThatDoesAllTheLogic(...); } public ??? MethodThatDoesAllTheLogic(...) { ... } </code></pre> <p>I've been fiddling around with casting and generics, but I just can't get it to work. Any ideas?</p> <p><strong>Edit</strong></p> <p>I have used millimoose's solution. Here's my code now:</p> <pre><code> public Dictionary&lt;string, string&gt; GenerateDictionary(...) { Dictionary&lt;string, string&gt; returnMap = new Dictionary&lt;string, string&gt;(); PopulateDictionary(returnMap.Add, ...); return returnMap; } public Multimap&lt;string, string&gt; GenerateMultimap(...) { Multimap&lt;string, string&gt; returnMap = new Multimap&lt;string, string&gt;(); PopulateDictionary(returnMap.Add, ...); return returnMap; } private static void PopulateGenericDictionary(Action&lt;string, string&gt; addFunc, ...) { ... foreach (...) { addFunc(key, value); } } </code></pre> <p>Much cleaner!</p>
    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.
 

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