C# 3.0 generic type inference - passing a delegate as a function parameter

后端 未结 5 1575
囚心锁ツ
囚心锁ツ 2020-11-27 05:34

I am wondering why the C# 3.0 compiler is unable to infer the type of a method when it is passed as a parameter to a generic function when it can implicitly create a delegat

5条回答
  •  日久生厌
    2020-11-27 05:59

    The reasoning is that if the type ever expands there should be no possibility of failure. i.e., if a method foo(string) is added to the type, it should never matter to existing code - as long as the contents of existing methods don't change.

    For that reason, even when there is only one method foo, a reference to foo (known as a method group) cannot be cast to a non-type-specific delegate, such as Action but only to a type-specific delegate such as Action.

提交回复
热议问题