Delegate Covariance Confusion Conundrum!

前端 未结 6 1969
孤街浪徒
孤街浪徒 2021-01-02 14:47

Why does this not work? Do I not understand delegate covariance correctly?

public delegate void MyDelegate(object obj)

public class MyClass
{
    public MyC         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 15:38

    The MyDelegate type declares that you can pass any kind of object in. However, MyMethod only takes objects of type SomeObject. What happens if I try to invoke the delegate passing a different kind of object: _delegate("a string object")? According to the declaration of MyDelegate, this should be allowed, but your function MyMethod can't actually receive a string argument.

提交回复
热议问题