title: How to check the parameters without enumerating infinite source in c# extension method
body: <p>I'm trying to write an NUnit test to check that my extension method throws an exception when called with a negative parameter, what i want is to execute the first two if condition to check the parameters <strong>without</strong> enumerating the soruce &quot;infiniteSeq&quot;, not even partially.</p>
  <p>I want this test to have success without using a toList or something to force the enumeration.</p>
  <pre><code>Assert.That(()=&gt; infiniteSeq.Smooth(-1), Throws.TypeOf&lt;ArgumentOutOfRangeException&gt;());
  </code></pre>
  <p>This is my extension method, it must never end, if it does then the source is finite and it throws a FiniteSourceException.</p>
  <pre><code>public static IEnumerable&lt;double&gt; Smooth(this IEnumerable&lt;double&gt; s, int N)
      {
          if (N &lt; 0) throw new ArgumentOutOfRangeException(nameof(N), &quot;Is negative&quot;);
          if (s is null) throw new ArgumentNullException(nameof(s), &quot;is null&quot;);
          
          while (true)
          {
              List&lt;double&gt; buffer = new List&lt;double&gt;();
              using (var seqEnumerator = s.GetEnumerator())
              {
                  int index = 0;
                  while (seqEnumerator.MoveNext())
                  {
                      buffer.Add(seqEnumerator.Current);
                          
                      //Enough items to start Smoothing
                      if (buffer.Count &gt;= N*2)
                      {
                          List&lt;double&gt; elementForAvg;
                          try
                          {
                              int startIndex = (index-N&lt;0) ? 0 : index-N;
                              int endIndex = index+N;
                              
                              elementForAvg = buffer.GetRange(startIndex,endIndex);
                          }
                          catch (Exception e)
                          {
                              if (e is ArgumentException)
                                  throw new FiniteSourceException();
                              
                              throw;
                          }
                          
                          yield return AvgCalculator(elementForAvg);
                          index++;
                      }
                  }
                  throw new FiniteSourceException();
              }
  </code></pre>
  <p>Is this possible?</p>