简体   繁体   中英

Generic constraints and interface implementation/inheritance

Not entirely sure how to phrase the question, because it's a "why doesn't this work?" type of query.

I've reduced my particular issue down to this code:

public interface IFoo
{
}

public class Foo : IFoo
{
}

public class Bar<T> where T : IFoo
{
    public Bar(T t)
    {
    }

    public Bar()
        : this(new Foo()) // cannot convert from 'Foo' to 'T'
    {
    }
}

Now, the generic type T in the Bar<T> class must implement IFoo. So why does the compiler give me the error in the comment? Surely an instance of Foo is an IFoo, and can therefore be passed around as a representative of the generic type T ?

Is this a compiler limitation or am I missing something?

You could also have a Fiz that implements IFoo that is not related to Foo in any other way:

public interface IFoo
{
}

public class Foo : IFoo
{
}

public class Fiz : IFoo
{
}

Foo foo = new Foo();
Fiz fiz = foo; // Not gonna compile.

What you want is probably more like:

public class Bar<T> where T : IFoo, new()
{
    public Bar(T t)
    {
    }

    public Bar()
        : this(new T()) 
    {
    }
}

So you can have

Bar<Foo> barFoo = new Bar<Foo>();
Bar<Fiz> barFiz = new Bar<Fiz>();

如果你创建一个类Baz,然后是泛型类型Bar baz = new Bar(),那么由构造函数重载定义的新Foo()将不是T类型,在本例中是Baz。

It's because if you create a class:

public class Fred : IFoo
{
}

And then instantiate Bar<T> like this:

var bar = new Bar<Fred>();

Then it violates the constraints of the class as a Foo is not a Fred which is the current T .

You can force it to compile by putting the cast sequence (T)(IFoo)new Foo() in the constructor, but you'll get an InvalidCastException at runtime if the actual type of T is not assignable from Foo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM