开发者

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

开发者 https://www.devze.com 2022-12-30 11:42 出处:网络
I am new to programming in Silverlight. Can anyone tell me the difference between FrameworkElement obj=sender as FrameworkElement

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

FrameworkElement obj=sender as FrameworkElement 

an开发者_如何转开发d

FrameworkElement obj=(FrameworkElement)someobject 


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

Casting and Type Conversions (C# Programming Guide)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号