Why doesn't delegate contravariance work with value types?

前端 未结 3 1271
礼貌的吻别
礼貌的吻别 2020-12-01 11:02

This snippet is not compiled in LINQPad.

void Main()
{
    (new[]{0,1,2,3}).Where(IsNull).Dump();
}

static bool IsNull(object arg) { return arg == null; }
<         


        
3条回答
  •  隐瞒了意图╮
    2020-12-01 11:53

    Because Int32 is value type and contra-variance doesn't work on value types.

    You can try this one:

    (new **object**[]{0,1,2,3}).Where(IsNull).Dump();
    

提交回复
热议问题