开发者

Why is the two parameter constructor called when single parameter constructor is expected?

开发者 https://www.devze.com 2023-01-11 23:15 出处:网络
I have the following two constructors in my base class: prote开发者_开发技巧cted ExternalSystemException( params Object[] args )

I have the following two constructors in my base class:

prote开发者_开发技巧cted ExternalSystemException( params Object[] args ) 
    : this( null, null, args ) {}

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

My child class has the following constructor:

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

My client logic instantiates the child class like so:

throw new ChildClass( "goofy" );

When I step through the logic I unexpectedly end up at the base constructor with the parameters ( String propertyKeySuffix, params Object[] args ). I expected the other base class constructor to be called, namely ( params Object[] args ).

Can someone tell me why this is happening?


The string overload is the best match to the type you are providing to the constructor. The Params are optional (and Object is ambiguous), so since the second overload has a string type that matches the string type you are passing, the second overload is selected.

0

精彩评论

暂无评论...
验证码 换一张
取 消