An event’s sender
is just passed to the event handler as an object. Now when that event is raised, you usually know what kind of sender you can expect (since you set up the event handler yourself), but the method still requires an object
type.
Now the as
is a type conversion that tries to convert the object into that type, but returns null
if the type is not compatible. So in this case, you have this:
var rectangle = sender as Rectangle;
There are two possibilities:
sender
is a type that can be assigned to aRectangle
, in which case,rectangle
will contain a reference to the same object but typed as aRectangle
instead of justobject
sender
is of some other type, in which caserectangle
will benull
, which is caught in the following check.