title: Handle variable number of out parameters with less code duplication in C#
body: <p>I'm trying to write a function that populates strings with the contents of an array, or sets them to null. The number of strings is can vary and I don't want to add requirements like them all being part of the same array or class.</p>
  
  <p>In C# you cannot combine <code>param</code> and <code>out</code>. Therefore the only way to do this seems to be to overload the method like this:</p>
  
  <pre><code>    public void ParseRemainders(string[] remainders, out string p1)
      {
          p1 = null;
          if ((remainders != null) &amp;&amp; (remainders.Length &gt; 0))
              p1 = remainders[0];
      }
  
      public void ParseRemainders(string[] remainders, out string p1, out string p2)
      {
          p1 = null;
          p2 = null;
          if (remainders != null)
          {
              ParseRemainders(remainders, out p1);
              if (remainders.Length &gt; 1)
                  p2 = remainders[1];
          }
      }
  
      public void ParseRemainders(string[] remainders, out string p1, out string p2, out string p3)
      {
          p1 = null;
          p2 = null;
          p3 = null;
          if (remainders != null)
          {
              ParseRemainders(remainders, out p1, out p2);
              if (remainders.Length &gt; 2)
                  p3 = remainders[2];
          }
      }
  
      .... and on forever ....
  </code></pre>
  
  <p>How can I avoid all this code duplication, ideally accepting an arbitrary number of parameters?</p>
  
  <hr>
  
  <p><strong>Edit:</strong> This is useful because you could do, say, <code>ParseRemainders(remainders, out inputFileName, out outputFileName, out configFileName)</code> and then avoid having to manually do</p>
  
  <pre><code>if (remainder.Length &gt; 0) inputFileName = remainder[0];
  if (remainder.Length &gt; 1) outputFileName = remainder[1];
  if (remainder.Length &gt; 2) configFileName = remainder[2];
  ...
  </code></pre>
  
  <p>Sorry if this wasn't clear, I had a specific goal in mind which I why I didn't simply return a <code>List&lt;&gt;</code>.</p>
  
  <hr>
  
  <p><strong>Conclusion:</strong> Thanks to Botond Balázs for the answer, particularly the hint that this is called "array destructuring". As they point out, and as this question confirms, it is not possible in the current version of C#: <a href="https://stackoverflow.com/questions/35754020/destructuring-assignment-object-properties-to-variables-in-c-sharp">Destructuring assignment - object properties to variables in C#</a></p>