Does it improve performance casting to lower level types?
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.
精彩评论