Just like the popular view, I thought lesser code means better performance.

I tested:
vb.net Code:
  1. value = CType(object, ObjectType).Property
against
vb.net Code:
  1. Dim obj As ObjectType = CType(object, ObjectType)
  2. value = obj.Property
The first method doesn't need an intermediate variable in between to store my object. This should also put less burden on the garbage collector as there is no variable. The 2nd way has an intermediate variable required in memory. So I thought it must be faster using the first way rather than the second way. But test results shows just the opposite here.

vb.net Code:
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.     Dim sw As New Stopwatch, senderName As String
  3.  
  4.     For i As Integer = 1 To 5
  5.         sw.Reset() : sw.Start()
  6.         For j As Integer = 1 To 100000000
  7.             senderName = CType(sender, Button).Name
  8.         Next
  9.         sw.Stop()
  10.         Debug.Print("One Line : " & sw.ElapsedMilliseconds)
  11.  
  12.         sw.Reset() : sw.Start()
  13.         For j As Integer = 1 To 100000000
  14.             Dim btn As Button = CType(sender, Button)
  15.             senderName = btn.Name
  16.         Next
  17.         sw.Stop()
  18.         Debug.Print("Two Lines: " & sw.ElapsedMilliseconds)
  19.     Next
  20. End Sub

This is the result:
Code:
One Line : 3786
Two Lines: 3677
One Line : 3787
Two Lines: 3683
One Line : 3787
Two Lines: 3677
One Line : 3790
Two Lines: 3678
One Line : 3783
Two Lines: 3677
Anyone know why this happens? Is the CLR doing some smart optimizations?

Pradeep