Why does one interface implementation require a cast while another does not?

Kyle Baran

I was doing some work with interfaces today, when I run into the following scenario. Given these two simple interfaces:

public interface IItem { }
public interface IInventory
{
    ICollection<IItem> Items { get; }
}

I made a simple class to implement IInventory, and noticed that this implementation is perfectly fine as written:

public class BasicInventory1 : IInventory
{
    private Dictionary<int, IItem> items;
    public ICollection<IItem> Items
    {
        get { return items.Values; }
    }
}

But yet, this implementation requires a cast:

public class BasicInventory2 : IInventory
{
    private Dictionary<int, IItem> items;
    public ICollection<IItem> Items
    {
        get { return (ICollection<IItem>)items; }
    }
}

Why does one require a cast and the other doesn't? Checking the object typing for both collections that are getting returned in either case confirms that they both in fact implement ICollection.

I suspect there is some magic type conversions going on under the hood here, and therefore seems to have something to do with co/contravariance, but I don't quite see what exactly is going on.

usr

Dictionary<int, IItem> does not implement ICollection<IItem>. Simple as that.

It wouldn't make sense to implement that interface because you cannot add to a dictionary without specifying a key. The interface does not make sense.

This is a runtime error because items could refer to a subclass of Dictionary so that the cast might be valid.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the implementation of a method from a interface cast parameters to Object(or extends YourObject)?

From Dev

Why does While loop return one variable but not another?

From Dev

Why does FFmpeg require nostdin in while loop?

From Dev

Why does lexical_cast require the operator>> to be in a matching namespace?

From Dev

Why does a wildcard on a generic parameter require an explicit cast?

From Dev

PHP OOP, why does one method call require the self keyword and another doesn't?

From Dev

Why does Amazon SES not require SPF modifications in their current implementation?

From Dev

WebAPI controller not working while another one does

From Dev

Why does this if require a default?

From Dev

Why does this if require a default?

From Dev

Why does the images not come one after another?

From Dev

Why does the images not come one after another?

From Dev

Why does one div is moving on another?

From Dev

Why does one iteration of my arraylist work, while the other does not?

From Dev

Why does one iteration of my arraylist work, while the other does not?

From Dev

Does dynamic_cast require virtual function?

From Dev

Interface does not see concrete implementation

From Dev

Does one child implementing an interface, but another not, violate the Liskov Substitution Principle?

From Dev

Why does accessing an instance attribute in tkinter require brackets, while accessing an instance attribute in pygame require dot?

From Dev

Why does Java require an explicit cast on a final variable if it was copied from an array?

From Dev

Why does Java require an explicit cast on a final variable if it was copied from an array?

From Dev

Why does java require a cast for the instantiation of a bounded type parameter to its upper bound class?

From Dev

Dagger : Why does dagger require a @inject constructor for an object that does't depend on another object

From Dev

Why does DynamoDB require expressionAttributeValue?

From Dev

Why does this if statement require brackets?

From Dev

Why does the JVM require warmup?

From Dev

Why does `postgres` require a shell?

From Dev

Why does this require an if statement to execute

From Dev

Why does this code require casting?

Related Related

  1. 1

    Why does the implementation of a method from a interface cast parameters to Object(or extends YourObject)?

  2. 2

    Why does While loop return one variable but not another?

  3. 3

    Why does FFmpeg require nostdin in while loop?

  4. 4

    Why does lexical_cast require the operator>> to be in a matching namespace?

  5. 5

    Why does a wildcard on a generic parameter require an explicit cast?

  6. 6

    PHP OOP, why does one method call require the self keyword and another doesn't?

  7. 7

    Why does Amazon SES not require SPF modifications in their current implementation?

  8. 8

    WebAPI controller not working while another one does

  9. 9

    Why does this if require a default?

  10. 10

    Why does this if require a default?

  11. 11

    Why does the images not come one after another?

  12. 12

    Why does the images not come one after another?

  13. 13

    Why does one div is moving on another?

  14. 14

    Why does one iteration of my arraylist work, while the other does not?

  15. 15

    Why does one iteration of my arraylist work, while the other does not?

  16. 16

    Does dynamic_cast require virtual function?

  17. 17

    Interface does not see concrete implementation

  18. 18

    Does one child implementing an interface, but another not, violate the Liskov Substitution Principle?

  19. 19

    Why does accessing an instance attribute in tkinter require brackets, while accessing an instance attribute in pygame require dot?

  20. 20

    Why does Java require an explicit cast on a final variable if it was copied from an array?

  21. 21

    Why does Java require an explicit cast on a final variable if it was copied from an array?

  22. 22

    Why does java require a cast for the instantiation of a bounded type parameter to its upper bound class?

  23. 23

    Dagger : Why does dagger require a @inject constructor for an object that does't depend on another object

  24. 24

    Why does DynamoDB require expressionAttributeValue?

  25. 25

    Why does this if statement require brackets?

  26. 26

    Why does the JVM require warmup?

  27. 27

    Why does `postgres` require a shell?

  28. 28

    Why does this require an if statement to execute

  29. 29

    Why does this code require casting?

HotTag

Archive