sthay
sthay

Reputation: 495

Casting null as an object?

I came across this code today

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

Is there anything wrong with it or no?

Upvotes: 26

Views: 10803

Answers (2)

Brian Genisio
Brian Genisio

Reputation: 48127

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.

Upvotes: 39

Jimmy
Jimmy

Reputation: 91462

it probably needs the cast to resolve overloads

Upvotes: 4

Related Questions