Delegate Covariance Confusion Conundrum!

前端 未结 6 1985
孤街浪徒
孤街浪徒 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:44

    Arguments are contravariant, return types are covariant. If the delegate were to be called with an object that is not an instance of SomeObject, you'd have a typing error. On the other hand, returning SomeObject from a routine wrapped in a delegate that returns object is fine.

提交回复
热议问题