c# - Checking for Null in Constructor -
i'm trying figure out best practices reusable code debugged. have ran common practice among developers don't quite understand yet.
public myconstructor(object myobject) { if (myobject == null) throw new argumentnullexception("myobject null."); _myobject = myobject; }
it seems unnecessary check. think it's because don't understand benefits of doing check are. seems null reference exception thrown anyway? wrong, hear thoughts on it.
thank you.
to compiler, null
legitimate constructor argument.
your class might able handle null value myobject
. if can't - if class break when myobject
null - checking in constructor allows fail fast.
Comments
Post a Comment