Why calling ISet.Contains() compiles, but throws an exception at runtime?

后端 未结 5 1853
时光取名叫无心
时光取名叫无心 2020-12-08 08:26

Please, help me to explain the following behavior:

dynamic d = 1;
ISet s = new HashSet();
s.Contains(d);

The

5条回答
  •  爱一瞬间的悲伤
    2020-12-08 08:40

    The answers you have received so far do not explain the behaviour you are seeing. The DLR should find the method ICollection.Contains(object) and call it with the boxed integer as a parameter, even if the static type of the variable is ISet instead of ICollection (because the former derives from the latter).

    Therefore, I believe this is a bug and I have reported it to Microsoft Connect. If it turns out that the behaviour is somehow desirable, they will post a comment to that effect there.

    提交回复
    热议问题