title: How to retrieve object from interface collection based on predicate specific to concrete class?
body: <p>I have a collection of objects as a property in my class implementing <code>IFoo</code>. I'd like to add a method for retrieval of an item from the collection by type and predicate (specific to the concrete implementation). The problem I have is that I can't downcast from collection defined by interface to my concrete class (type cannot be inferred from usage) like so:</p>
  
  <pre><code>public HashSet&lt;IFoo&gt; Foos { get; } = new HashSet&lt;IFoo&gt;();
  
  public T GetFoo&lt;T&gt;(Func&lt;T, bool&gt; predicate) where T : class
      {
          if (Foos != null &amp;&amp; predicate != null)
          {
              var foos = Foos.Where(f =&gt; f is T);
              return foo = foos.FirstOrDefault(predicate);
          }
  
          return null;
      }
  </code></pre>
  
  <p>How can I do this?</p>