title: Passing a delegate with two parameters as a parameter function
body: <p>I have a sequence of functions that look very similar but for a single line, like the following two (but I have many more of them):</p>
  
  <pre><code>private static int HowManyHoursInTheFirstYear(IList&lt;T&gt; samples)
  {
      DateTime firstDate = samples[0].Date;
      int count = 0;
  
      while (count &lt; samples.Count &amp;&amp; 
            samples[count].Date.Year == firstDate.Year)
      {
          count++;
      }
  
      return count;
  }
  
  
  private static int HowManyDaysInTheFirstMonth(IList&lt;T&gt; samples)
  {
      DateTime firstDate = samples[0].Date;
      int count = 0;
  
      while (count &lt; samples.Count &amp;&amp; 
             samples[count].Date.Month == firstDate.Month) // &lt;--- only change!
          count++;
      }
  
      return count;
  }
  </code></pre>
  
  <p>I was thinking about using delegates to remove this repetition in code in some elegant way, that would have allowed me to invoke something like:</p>
  
  <pre><code>HowManyDaysInTheFirstPeriod(
      samples,
      delegate(DateTime d1, DateTime d2) { return d1.Month == d2.Month; });
  </code></pre>
  
  <p>thereby declaring a delegate like the following:</p>
  
  <pre><code>delegate bool DateComparer(DateTime first, DateTime second);
  </code></pre>
  
  <p>and where HowManyDaysInTheFirstPeriod whould be something like the following:</p>
  
  <pre><code>private static int HowManySamplesInFirstPeriod
      IList&lt;T&gt; samples,
      DateComparer comparer)
  {
      DateTime firstDate = samples[0].Date;
      int count = 0;
  
      while (count &lt; samples.Count &amp;&amp; comparer())
      {
          count++;
      }
  }
  </code></pre>
  
  <p>Unfortunately, the compiler complains that comparer needs two parameters. </p>
  
  <p>I am relatively new to C# and hit a road-block here. 
  How would you solve this?</p>