I came across this code today
AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null);
Is there anything wrong with it or no?
1 Answer. You can cast null to any source type without preparing any exception. The println method does not throw the null pointer because it first checks whether the object is null or not.
You can cast null to any reference type without getting any exception. The println method does not throw null pointer because it first checks whether the object is null or not. If null then it simply prints the string "null" . Otherwise it will call the toString method of that object.
No , null is not an object.It is a reference type and its value does not refer to any object and so there is no representation of null in memory.
Since null is a valid value for all reference types, as long as the cast route exists you should be fine.
Sometimes, you need to to this when the method is overloaded... to tell the compiler which one you are calling. A null object is still null and it is safe.
it probably needs the cast to resolve overloads
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