Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While both statements are saying essentially the same thing, the minor semantic difference does have at least one maintenance effect.</p> <pre><code>class A&lt;T&gt; where T : IFoo {} </code></pre> <p>This says that A will work on any type, so long as it has IFoo implemented.</p> <pre><code>class B&lt;IFoo&gt; {} </code></pre> <p>This says that B will only work on types that implement IFoo.</p> <p>The distinction is that you can still expand A to also require other things, like that IBar also be implemented, so you can do more things in A.</p> <p>B on the other hand works better as a base class, <code>B&lt;T&gt;</code>, and then you can extend <code>C&lt;IFoo&gt;</code> from it. C is now a B that works on IFoo. Later on, you could extend class <code>D&lt;IBar&gt;</code> to have a B that works on only IBars (not both IBar and IFoo). </p> <p>So you'd use the first form if you're writing one class that always needs to work on IFoo. This use case is pretty common, maybe writing a helper class for some interface types you wrote. You'd use the second form if you're writing a generic base class that can work on anything that you specify the interface for. </p> <p>I've used this form a fair bit, for example I just wrote a generic abstract serializer to handle the basic operations of serialization, but leaving abstract methods (like <code>int GetKeyField(T record)</code>) in it (the template method pattern) to allow specialization. So then I can have <code>IFooSerialize : Serializer&lt;IFoo&gt;</code> that serializes IFoos according to what is specific about IFoo.</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.
 

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