title: c# 8 switch expression: No best type was found for the switch expression
body: <p>I have added a code in my startup class (.net core 3.1) to return the type based on parameter and I get compile-time errors.</p>
  
  <p>I have created a running example in <a href="https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+ABATARgFgAoEgOwjDigAcIBjOAAgGEB7MqNgGzgEEaNIsQDeJJhKYBLMvAQAzBswCSACQhkUvBExFMAvuMnYAzEzxMAQtDjrN23QaMTT53EwCyATztbE+ECsbX20kJjUNPx09Q2JJczMLbxDEXEDrKFtI0PCU6KdSOON3bHwAdmddSvjSgDZzABZPCBkAClKABgBtAF0mCAQAcygASmrJMUL46ckANwGmAGs4LyYAXiYAIjAvAAts/02AbhJxmfmdfftEdaWVpigAdykYel2z6cmZ78ltvYOEPhNusAHxMMhwR6eHwA/CtEZID4/P5XKK4YFrMEQqHJAG4eGIoo/GYAfVBTBguwQbCh2KYADk2DBlGAaLwqLI4CgAKIAD0YNBgUg48KRkn0JymxIkFyYvNuy1WTxebzFEi+0umKNhGLBmy4VEpMkGm0Jmq1O1R2nR5P1bENu2NTDg3EyprV0zJmK2KDgigArtwYEx6DZNh6JacicTSgBOVqbVQu7hsJgAdTYCG4KAAhJsRpKfrF4rFYkA==" rel="noreferrer">sharplab</a>. if switch expression contains the string or other objects it runs fine.</p>
  
  <p>working example 1:</p>
  
  <pre><code>var x = key switch
              {
                  "myhandler1" =&gt; "something",
                  "myhandler2" =&gt; "something else",
                  _ =&gt; "default case"
              };
  </code></pre>
  
  <p>working example 2:</p>
  
  <pre><code>object obj =  s switch {
              "a" =&gt; new object(),
              "b" =&gt; new DateTime(),
              _ =&gt; throw new NotImplementedException()
          };
  </code></pre>
  
  <p><strong><em>Error example:</em></strong></p>
  
  <pre><code>interface IHandler { }
  public class BaseHandler { }
  public class MyHandler1: BaseHandler, IHandler { }
  public class MyHandler2: BaseHandler, IHandler { }
  
  class Program
  {
      static void Main(string[] args)
      {
  
          var key = "myhandler1";
  
          var handler = key switch
          {
              "myhandler1" =&gt; new MyHandler1(),
              "myhandler2" =&gt; new MyHandler2(),
              _ =&gt; throw new NotImplementedException()
          };
  
          var x = key switch
          {
              "myhandler1" =&gt; "something",
              "myhandler2" =&gt; "something else",
              _ =&gt; "default case"
          };
  
          Console.WriteLine("Hello World!");
      }
  }
  </code></pre>
  
  <p><strong><em>original problem (needs fixing):</em></strong></p>
  
  <pre><code>serviceCollection.AddTransient&lt;Func&lt;string, IHandler&gt;&gt;(sp =&gt; key =&gt;
              {
                  return key switch
                  {
                      Constants.Brand =&gt; sp.GetService&lt;Handler1&gt;(),
                      Constants.Series =&gt; sp.GetService&lt;Handler2&gt;(),
                      _ =&gt; throw new NotImplementedException()
  
                  };
  }
  </code></pre>
  
  <p>found this link: <a href="https://github.com/dotnet/csharplang/issues/2728" rel="noreferrer">https://github.com/dotnet/csharplang/issues/2728</a></p>
  
  <p>Thanks to <strong>Pavel</strong> and <strong>Marc</strong>, below is the fix:</p>
  
  <pre><code>serviceCollection.AddTransient&lt;Func&lt;string, IHandler&gt;&gt;(sp =&gt; key =&gt;
              {
                  return key switch
                  {
                      Constants.Brand =&gt; (sp.GetService&lt;Handler1&gt;() as IHandler),
                      Constants.Series =&gt; (sp.GetService&lt;Handler2&gt;() as IHandler),
                      _ =&gt; throw new NotImplementedException()
  
                  };
  }
  </code></pre>