Sometimes I have to implement an interface or inherit a virtual (MustInherit) that the base method expects an object, whilst I know that the value I will be passing will always be an Integer for example.
What should be the best performance from the examples below:
Public Sub DoSomething(ByVal obj As Object)
'option 1:
Dim x As Integer = obj
'option 2:
Dim y = DirectCast(obj, Integer)
End Function
Considerations:
- Option 1: No casting but is maybe not so proper, does it cost less performance?
- Option 2: Casting when the type is known, but feels more safe.
Note: Please don't comment with "Why wouldn't you want to implement it in different way" etc. etc. My question is not how to do this, I didn't find an example of how to ask it, my question is just what option should be the rightes, and what will cost more performance.