Can anyone tell me the difference between FrameworkElement obj=sender as FrameworkElement and FrameworkElement obj=(FrameworkElement) someobject
-
27-09-2019 - |
Question
I am new to programming in Silverlight. Can anyone tell me the difference between
FrameworkElement obj=sender as FrameworkElement
and
FrameworkElement obj=(FrameworkElement)someobject
Solution
FrameworkElement obj=sender as FrameworkElement
after this code obj will be FrameworkElement, if type of it is FrameworkElement, or null, in other cases. This code will not throw InvalidCastException.
FrameworkElement obj=(FrameworkElement)sender
this is explicit conversion, and this operation can throw an InvalidCastException
OTHER TIPS
Yes, the difference is
FrameworkElement obj=sender as FrameworkElement
always works. If sender is not of type FrameworkElement, obj is null, otherwise you will find the casted object in there.
FrameworkElement obj=(FrameworkElement)someobject
produces an InvalidCastException if sender cannot be casted to type FrameworkElement.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow