title: Pattern matching case when
body: <p>I write simple math tokenizer and try to use new C# <code>pattern matching</code> feature.</p>
  
  <p>Tokenizer is quite simple:</p>
  
  <pre><code>    public IEnumerable&lt;IToken&gt; Tokenize(string input)
      {
          const char decimalSeparator = '.';
          string inputWithoutSpaces = input.Replace(" ", string.Empty);
          var numberBuffer = new StringBuilder();
          var letterBuffer = new StringBuilder();
          foreach (char c in inputWithoutSpaces)
          {
              switch (c)
              {
                  case var _ when IsTerm(c, letterBuffer):
                      if (numberBuffer.Length &gt; 0)
                      {
                          yield return EmptyNumberBufferAsLiteral(numberBuffer);
                          yield return new Operator('*');
                      }
                      letterBuffer.Append(c);
                      break;
                  case decimalSeparator:
                  case var _ when IsDigit(c):
                      numberBuffer.Append(c);
                      break;
                  case var _ when IsOperator(c):
                      if (numberBuffer.Length &gt; 0)
                      {
                          yield return EmptyNumberBufferAsLiteral(numberBuffer);
                      }
                      if (letterBuffer.Length &gt; 0)
                      {
                          yield return EmptyLetterBufferAsTerm(letterBuffer);
                      }
                      yield return new Operator(c);
                      break;
              }
          }
          if (numberBuffer.Length &gt; 0)
          {
              yield return EmptyNumberBufferAsLiteral(numberBuffer);
          }
          if (letterBuffer.Length &gt; 0)
          {
              yield return EmptyLetterBufferAsTerm(letterBuffer);
          }
      }
  </code></pre>
  
  <p>I'm using <code>case var _</code> because I want to match by condition without using <code>if-else if</code> chain, but I'm unable to write <code>case when</code> without specifying <code>var variableName</code>. </p>
  
  <p>Is there any fancy way to perform such operation? Or it is recommended way to do these things?</p>