title: Passing dictionary as parameter
body: <blockquote>
    <p><strong>The Setup</strong></p>
  </blockquote>
  
  <p>Let's say you have the following code</p>
  
  <pre><code>public Dictionary&lt;string,string&gt; BuildSettings(){
      var result = new Dictionary&lt;string,string&gt;();
  
      result.Add("key1","value1");
      result.Add("key2","value2");
      //And so on...
      //Add a LOT more items to result
  
      return result;
  }
  </code></pre>
  
  <blockquote>
    <p><strong>The Question</strong></p>
  </blockquote>
  
  <p>Because there are a lot of items in the dictionary, I want to split them up.
  BUT... What is the "clean code" way of passing the dictionary to the submethods  (and returning it to the parent method)? What are the pros &amp; cons from the options below?</p>
  
  <blockquote>
    <p><strong>Possible answers</strong></p>
  </blockquote>
  
  <p><strong>OPTION 1</strong>: Passing as parameter and returning dictionary</p>
  
  <pre><code>public Dictionary&lt;string,string&gt; BuildSettings(){
      var result = new Dictionary&lt;string,string&gt;();
  
      result = SubMethod1(result);
      result = SubMethod2(result);
      //And so on...
  
      return result;
  }
  public Dictionary&lt;string,string&gt; SubMethod1(Dictionary&lt;string,string&gt; result){
      result.Add("key1","value1");
      result.Add("key2","value2");
      //And so on...
  
      return result;
  }
  </code></pre>
  
  <p><strong>OPTION 2</strong>: Passing as parameter <strong>--> MOST POPULAR ANSWER</strong></p>
  
  <pre><code>public Dictionary&lt;string,string&gt; BuildSettings(){
      var result = new Dictionary&lt;string,string&gt;();
  
      SubMethod1(result);
      SubMethod2(result);
      //And so on...
  
      return result;
  }
  public void SubMethod1(Dictionary&lt;string,string&gt; result){
      result.Add("key1","value1");
      result.Add("key2","value2");
      //And so on...
  }
  </code></pre>
  
  <p><strong>OPTION 3</strong>: Passing as parameter with "ref" to indicate a change in dictionary</p>
  
  <pre><code>public Dictionary&lt;string,string&gt; BuildSettings(){
      var result = new Dictionary&lt;string,string&gt;();
  
      SubMethod1(ref result);
      SubMethod2(ref result);
      //And so on...
  
      return result;
  }
  
  public void SubMethod1(ref Dictionary&lt;string,string&gt; result){
      result.Add("key1","value1");
      result.Add("key2","value2");
      //And so on...
  }
  </code></pre>
  
  <p><strong>OPTION 4</strong>: Return new instance of Dictionary and merge dictionaries</p>
  
  <pre><code>public Dictionary&lt;string,string&gt; BuildSettings(){
      var result = new Dictionary&lt;string,string&gt;();
  
      foreach(var pair in SubMethod1()){
          result.Add(pair.Key, pair.Value);
      }
      foreach(var pair in SubMethod2()){
          result.Add(pair.Key, pair.Value);
      }
      //And so on...
  
      return result;
  }
  
  public Dictionary&lt;string,string&gt; SubMethod1(){
      var result = new Dictionary&lt;string,string&gt;();
  
      result.Add("key1","value1");
      result.Add("key2","value2");
      //And so on...
  
      return result;
  }
  </code></pre>
  
  <p><strong>OPTION 5</strong>: Extension method</p>
  
  <pre><code>public Dictionary&lt;string,string&gt; BuildSettings(){
      var result = new Dictionary&lt;string,string&gt;();
  
      result.SubMethod1();
      result.SubMethod2();
      //And so on...
  
      return result;
  }
  
  public Dictionary&lt;string,string&gt; SubMethod1(this Dictionary&lt;string,string&gt; result){
      result.Add("key1","value1");
      result.Add("key2","value2");
      //And so on...
  
      return result;
  }
  </code></pre>
  
  <p><strong>OPTION 6</strong>: Dictionary as field in class</p>
  
  <pre><code>private Dictionary&lt;string,string&gt; _settings = new Dictionary&lt;string,string&gt;();
  
  public Dictionary&lt;string,string&gt; BuildSettings(){
  
      SubMethod1();
      SubMethod2();
      //And so on...
  
      return settings;
  }
  
  public void SubMethod1(){
      settings.Add("key1","value1");
      settings.Add("key2","value2");
      //And so on...
  }
  </code></pre>
  
  <p><strong>OPTION 7</strong>: Other suggestions?</p>
  
  <blockquote>
    <p><strong>Result</strong></p>
  </blockquote>
  
  <p>I think option 2 is best, like most people here indicate.
  Additionally, I think adding a good method name could also help with this.</p>
  
  <p>So this should be the code (in my opinion):</p>
  
  <pre><code>public Dictionary&lt;string,string&gt; BuildSettings(){
      var result = new Dictionary&lt;string,string&gt;();
  
      AddUserSettingsTo(result);
      AddCompanySettingsTo(result);
      //And so on...
  
      return result;
  }
  
  public void AddUserSettingsTo(Dictionary&lt;string,string&gt; result){
      result.Add("key1","value1");
      result.Add("key2","value2");
      //And so on...
  }
  </code></pre>