Can anyone tell me the difference between FrameworkElement obj = sender as FrameworkElement and FrameworkElement obj = (FrameworkElement) someobject

I am new to Silverlight programming. Can anyone tell me the difference between

FrameworkElement obj=sender as FrameworkElement 

      

and

FrameworkElement obj=(FrameworkElement)someobject 

      

+2


a source to share


2 answers


FrameworkElement obj=sender as FrameworkElement 

      

after this code, obj will be FrameworkElement if its type is FrameworkElement or null otherwise. This code will not throw InvalidCastException .

FrameworkElement obj=(FrameworkElement)sender

      



this is an explicit conversion and may throw an InvalidCastException

Cast and Type Conversion (C # Programming Guide)

+2


a source


Yes, the difference

FrameworkElement obj=sender as FrameworkElement

always works. If the sender is not of type FrameworkElement, obj is NULL, otherwise you might find a cast object there.



FrameworkElement obj=(FrameworkElement)someobject

Throws an InvalidCastException if the sender cannot be started to type FrameworkElement.

0


a source







All Articles