Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to cast constrained generic interface reference to covariant interface reference

I’m trying to understand how covariance works with generic type constraints and it seems like the constraints are ignored for no obvious reason.

Consider the following code:

    public interface IContainer<out T> { }

    public interface IContents { }

    public class Food : IContents { }

    public class Foo
    {
        public void Bar<T>() where T : IContents
        {
            IContainer<IContents> x = null;
            IContainer<T> y = null;
            IContainer<Food> z = null;
            x = y; // Cannot convert source type 'IContainer<T>' to target type 'IContainer<IContents>'
            x = z; // Valid
        }
    }

Why does compiler produce ‘Cannot convert’ error on x = y and produce no error on x = z?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

C# does not support variance for value types – from variant generic interfaces doc:

ref, in, and out parameters in C# cannot be variant. Value types also do not support variance.

I.e. next will not work:

public struct MyStruct : IContents
{
}

var structContainer = (IContainer<MyStruct>)null;
IContainer<IContents> x = structContainer;

Since T is not constrained neither to class not to struct it can be either reference type or value type, so in general case IContainer<T> is not assignable to IContainer<IContents>.

Add class constraint to the Bar method:

public class Foo
{
    public void Bar<T>() where T : class, IContents
    {
        IContainer<IContents> x = null;
        IContainer<T> y = null;
        IContainer<Food> z = null;
        x = y; // Now Valid
        x = z; // Valid
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading