Casting null as an object?

2019-06-14 21:52发布

问题:

I came across this code today

AsyncInvoke(OnTimeMessageTimer, (object)null, (ElapsedEventArgs)null);

Is there anything wrong with it or no?

回答1:

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.



回答2:

it probably needs the cast to resolve overloads