Not in all cases.
Have the look at the IL for the following code (when compiled in release
mode):
-----------------
Sub Main()
Dim b As String = "hello"
Dim a As Object = b
Dim c As String
c = CType(a, String) ' Method 1
c = DirectCast(a, String) ' Method 2
c = CStr(a) ' Method 3
End Sub
------------
Both Method 1 and Method 3 do the same thing (a call to
Microsoft.VisualBasic.CompilerServices.StringType: :FromObject)
Method 2 (directcast) uses the "castclass" instruction directly.
Admittedly, the first thing
Microsoft.VisualBasic.CompilerServices.StringType: :FromObject seems to do is
check if it is already a string type and then call "castclass" and return,
but there is still a small overhead in calling the extra function in the
first place.
Not that optimizations like this will make much difference unless involved
in a mad loop of some sort. Still, I think it is better for the developer to
know the difference between when they *can* use directcast and when they
*have* to use ctype.
HTH,
Trev.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:OF**************@TK2MSFTNGP11.phx.gbl...
"Crirus" <Cr****@datagroup.ro> schrieb I gues it does for speed sake
Nope. If CType casts (not converts) it does *exactly* the same.
--
Armin
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html