Help Center

Local Navigation

Using caution when passing null into a constructor

To avoid ambiguity when passing null into a constructor, cast null to the appropriate object.

If a class has two or more constructors, passing in a null parameter might not uniquely identify which constructor to use. As a result, the compiler reports an error.

By casting null to the appropriate object, you indicate precisely which constructor the compiler should use. This practice also provides forward compatibility if later releases of the API add new constructors.

Code sample

new someObject ((someObject)null );


Was this information helpful? Send us your comments.