title: C# ternary casting with IEnumerable<Interface>
body: <p>this isn't a problem.  I just want to understand why it works this way.  In the real scenario, the IEnumerable is generated by SimpleInjector thru RegisterCollection if that helps.</p>
  
  <p>This simulator code shows 3 implementations of an interface and the selection of one of those instances based on a parameter.  I haven't even coded a call to the method because my question is about the compilation errors.</p>
  
  <pre><code>void Main()
  {
  
  }
  
  // Define other methods and classes here
  public interface ITransport
  {
      void Play();
      bool Pause();
      int Rewind();
      int Ffwd();
      void Stop();
      void Eject();
  }
  
  public class PlayerBase : ITransport
  {
      public string Name;
  
      public void Play()
      {
          throw new NotImplementedException();
      }
  
      public bool Pause()
      {
          throw new NotImplementedException();
      }
  
      public int Rewind()
      {
          throw new NotImplementedException();
      }
  
      public int Ffwd()
      {
          throw new NotImplementedException();
      }
  
      public void Stop()
      {
          throw new NotImplementedException();
      }
  
      public void Eject()
      {
          throw new NotImplementedException();
      }
  }
  
  public class TapePlayer : PlayerBase { }
  public class CDPlayer : PlayerBase {}
  public class EightTrackPlayer : PlayerBase{}
  public interface IDependencyRegistry { T Get&lt;T&gt;();}
  public class DependencyRegistry&lt;T&gt; : List&lt;T&gt;, IDependencyRegistry
  {
      public T1 Get&lt;T1&gt;()
      {
          if (typeof(T).IsInterface &amp;&amp; !typeof(T1).GetInterfaces().Contains(typeof(T))) return default(T1);
          return (T1) (object) this.SingleOrDefault(thing =&gt; thing.GetType() ==typeof(T1));
      }
  }
  public class PlayBase
  {
      public DependencyRegistry&lt;ITransport&gt; TransportRegistry
      public PlayBase(IEnumerable&lt;ITransport&gt; transports)
      {
          TransportRegistry = new UserQuery.DependencyRegistry&lt;UserQuery.ITransport&gt;();
          TransportRegistry.AddRange(transports);
      }
  
      public void Play(int playerId)
      {
          var player1 = playerId == 1 ? (ITransport)TransportRegistry.Get&lt;TapePlayer&gt;() : playerId == 2 ? TransportRegistry.Get&lt;CDPlayer&gt;() : null;
      }
  }
  </code></pre>
  
  <p>If you paste this into LinqPad5, it'll compile without error but remove the cast to ITransport and the complaint is that it can't figure out that both concretions stem from the same base and therefore the var (to my ignorant mind) should be assigned as PlayerBase or ITransport.</p>
  
  <p>I'm also confused that only 1 cast is need to satisfy the compiler.  If it can figure out the other concretion should also be cast to ITransport, then why can it not deduce either ITransport or PlayerBase from the hierarchy?</p>