Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main difference in your 2 examples is that in <code>class A</code> whenever you define a variable as T you can use all properties/functions on that variable that are also defined in IFoo.</p> <p>However in <code>class B</code> the <code>IFoo</code> is just a name for the generic type parameter and thus whenever you declare a variable inside the class as <code>IFoo</code> you can only use it as if it's an <code>object</code> type.</p> <p>for example if</p> <pre><code>public interface IFoo { int Value { get; set; } } </code></pre> <p>then you can do this in <code>class A</code></p> <pre><code>class A&lt;T&gt; where T : IFoo { public void DoSomething(T value) { value.Value++; } } </code></pre> <p>While if you'd try the same in <code>class B</code> you'll get a compiler error that the type <code>IFoo</code> does not contain a property <code>Value</code> or something similar. The reason is that the <code>&lt;IFoo&gt;</code> in class B is just a name and has no relation to the interface, you could've called it anything you like.</p> <p>Update:</p> <pre><code>class B { private IFoo foo; B(IFoo foo) { this.foo = foo; } /* etc */ } </code></pre> <p>This construct is indeed basically the same, except when you expose <code>IFoo</code> back to the outside again, Consider the following property in both classes</p> <p>class A:</p> <pre><code>public T Foo { get { return foo; }} </code></pre> <p>class B:</p> <pre><code>public IFoo Foo { get { return foo; }} </code></pre> <p>Now consider you initialized both classes with a class C which is defined as</p> <pre><code>public class FooClass : IFoo { public int Value { get; set; } public int SomeOtherValue { get; set; } } </code></pre> <p>then consider 2 variables defined as</p> <pre><code>var foo = new FooClass(); var a = new A&lt;FooClass&gt;(foo); var b = new B(foo); </code></pre> <p>now to set the <code>SomeOtherValue</code> using <code>a</code> you can do </p> <pre><code>a.Foo.SomeOtherValue = 2; </code></pre> <p>while for b you have to do</p> <pre><code>((FooClass)b.Foo).SomeOtherValue = 2; </code></pre> <p>Hope that makes sense ;-)</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