I think it depends on how exceptional null
as the value for the corresponding argument.
Compare, for example, this ArgumentNullException
constructor : it is most often called when it is necessary to set an internal exception. Otherwise, this constructor is passed, which excludes the name of the illegal argument. At odd moments, the first one should be called because a custom message needs to be provided, but no internal exception is thrown (usually I do this when I throw an exception for the array / collection argument containing null
but not null
). So, in this case, I need an explicit cast, and I would say that it is acceptable there.
If your methods really do the same, but null
is still the usual value, you might want to add parameterless overloading for the null
option (i.e. explicit casting is still possible, but users can also call without parameters).
If your methods do something slightly different and something else for null
, you might consider completely rejecting null
for the methods you specified and adding parameterless overload for the null
case.
Update: If null
in any case not acceptable (and will result in an exception), you should leave the method as it is. Apart from the purpose of testing, there should never be any situation in which a null
literal will be passed to a method, as this will inevitably lead to an exception. Therefore, do not change the overload names in this case.
source share