Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wanted to create an extension method that could enumerate over a list of things, and return a list of those things that were of a certain type. It would look like this:</p> <pre><code>listOfFruits.ThatAre&lt;Banana&gt;().Where(banana =&gt; banana.Peel != Color.Black) ... </code></pre> <p>Sadly, this is not possible. The proposed signature for this extension method would have looked like:</p> <pre><code>public static IEnumerable&lt;TResult&gt; ThatAre&lt;TSource, TResult&gt; (this IEnumerable&lt;TSource&gt; source) where TResult : TSource </code></pre> <p>... and the call to ThatAre&lt;> fails because both type arguments need to be specified, even though TSource may be inferred from the usage.</p> <p>Following the advice in other answers, I created two functions: one which captures the source, and another which allows callers to express the result:</p> <pre><code>public static ThatAreWrapper&lt;TSource&gt; That&lt;TSource&gt; (this IEnumerable&lt;TSource&gt; source) { return new ThatAreWrapper&lt;TSource&gt;(source); } public class ThatAreWrapper&lt;TSource&gt; { private readonly IEnumerable&lt;TSource&gt; SourceCollection; public ThatAreWrapper(IEnumerable&lt;TSource&gt; source) { SourceCollection = source; } public IEnumerable&lt;TResult&gt; Are&lt;TResult&gt;() where TResult : TSource { foreach (var sourceItem in SourceCollection) if (sourceItem is TResult) yield return (TResult)sourceItem; } } } </code></pre> <p>This results in the following calling code:</p> <pre><code>listOfFruits.That().Are&lt;Banana&gt;().Where(banana =&gt; banana.Peel != Color.Black) ... </code></pre> <p>... which isn't bad.</p> <p>Notice that because of the generic type constraints, the following code:</p> <pre><code>listOfFruits.That().Are&lt;Truck&gt;().Where(truck =&gt; truck.Horn.IsBroken) ... </code></pre> <p>will fail to compile at the Are() step, since Trucks are not Fruits. This beats the provided .OfType&lt;> function: </p> <pre><code>listOfFruits.OfType&lt;Truck&gt;().Where(truck =&gt; truck.Horn.IsBroken) ... </code></pre> <p>This compiles, but always yields zero results and indeed doesn't make any sense to try. It's much nicer to let the compiler help you spot these things.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload