为什么在期望单个参数构造函数时调用两个参数构造函数?

时间:2010-08-18 21:19:51

标签: c#

我的基类中有以下两个构造函数:

protected ExternalSystemException( params Object[] args ) 
    : this( null, null, args ) {}

protected ExternalSystemException( String propertyKeySuffix, 
    params Object[] args ) : this( propertyKeySuffix, null, args ) {}

我的子类有以下构造函数:

public InvalidPathToOutputFiles(string invalidPath) 
    : base(invalidPath) {}

我的客户端逻辑实例化子类,如下所示:

throw new ChildClass( "goofy" );

当我逐步执行逻辑时,我意外地以参数( String propertyKeySuffix, params Object[] args )结束了基本构造函数。我期望调用另一个基类构造函数,即( params Object[] args )

有人能告诉我为什么会这样吗?

1 个答案:

答案 0 :(得分:8)

string重载与您为构造函数提供的类型最匹配。 Params是可选的(并且Object是不明确的),因此第二个重载具有与您传递的string类型匹配的string类型,所以选择了第二个重载。