I am creating a new object
myobject t = new myobject();
should I check on the next line for null reference , if the new succeeded ?
if (null != t)
or I can be sure that this object for sure will be different then null ...
Thanks.
According to the C# documentation, if new
fails to successfully allocate space for a new object instance, an OutOfMemoryException
will be thrown. So there's no need to do an explicit check for null
.
If you are trying to detect cases where new
has failed to allocate a new object, you might want to do something like:
try {
myobject t = new myobject();
//do stuff with 't'
}
catch (OutOfMemoryException e) {
//handle the error (careful, you've run out of heap space!)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With