How do you check if classinstance
has been created/initialised or is null
?
private MyClass myclass;
if(!check class has been initialised) //<- What would that check be?
myclass = new MyClass();
Thanks in advance
Just check if it is null
if (myclass == null)
As mentioned in your comment,
if (myclass.Equals(null))
will not work because if myclass is null, that's translated to
if (null.Equals(null))
which leads to a NullReferenceException
.
By the way, objects declared in class scope are automatically initialized to null by the compiler, so your line:
private MyClass myclass;
is the same as
private MyClass myclass = null;
Objects declared in a method are forced to be assigned some initial value (even if that initial value is null).
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