开发者

Does it improve performance casting to lower level types?

开发者 https://www.devze.com 2023-01-25 17:28 出处:网络
Consider this examlpe Interface IInterface Property MyProperty End Interface Class MyClassy : Implements IInterface

Consider this examlpe

Interface IInterface
    Property MyProperty
End Interface

Class MyClassy : Implements IInterface
    Public Property MyProperty
End Class

Class MyClassy2 : Inherits MyClassy
End MyClassy
Class MyClassy3 : Inherits MyClassy
End MyClassy    
Class MyClassy4 : Inherits MyClassy
End MyClassy
Class MyClassy5 : Inherits MyClassy
End MyClassy

Private Sub MyHandler(sender As Object, e As EventArgs)
    'Here is my question:

    Dim lowLevel = DirectCast(sender, IInterface).MyProperty

    'vs.

    Dim 开发者_JAVA百科highLevel = DirectCast(sender, MyClassy5).MyProperty  

End Sub

The type of sender in the above example is of type MyClassy5.

Is there any performance differences between the two types of casting?


Because DirectCast does not use any runtime helpers to do the cast, the performance should be the same. But you should not care about that unless you do millions of casts per second.

0

精彩评论

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