C# Method overload resolution not selecting concrete generic override

前端 未结 3 695
轻奢々
轻奢々 2020-12-03 06:20

This complete C# program illustrates the issue:

public abstract class Executor
{
    public abstract void Execute(T item);
}

class StringExecutor :         


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-03 07:10

    As mentioned in Jon Skeet's article on overloading, when invoking a method in a class that also overrides a method with the same name from a base class, the compiler will always take the in-class method instead of the override, regardless of the "specificness" of type, provided that the signature is "compatible".

    Jon goes on to point out that this is an excellent argument for avoiding overloading across inheritance boundaries, since this is exactly the kind of unexpected behavior that can occur.

提交回复
热议问题