Which is more efficient Cstr(value) or value.ToString()

From here (couldn't say it any better):

CStr is a keyword, whereas ToString is a function (method). CStr is compiled inline and it creates code depending on the type of the passed object. It's mainly there for people being used to it from previous VB versions. I haven't used CStr in .Net anymore (because it's not obvious what it does in which situations and it's also not very well documented).

The difference depends on which ToString function you use. Every type can have it's own implementation.


For readability sake, if I were in the need to write code in VB.NET (I am a C# programmer), I would avoid the VB specific keywords/functions as much as possible. By using .NET classes and methods only, your code will be more understandable by people used to develop in other .NET languages. Not to mention that these functions are there mainly for compatibility with VB6 and they look a bit out of place compared to the .NET way of doing things.

Of course there may be reasonable exceptions, sometimes VB.NET really makes very easy to do certain tasks and it can be convenient to take advantage of this; but as a genereal rule I would not use any VB.NET specific function.


Here is the code from the above test, but redone with System.Diagnostics.Stopwatch, and removing the Console.write bottleneck.

It turns out directcasting is fastest(as well it should be - it isn't doing anything in this case. However its a trivialised example because its rare you would want to convert a string to a string. Converting Module Module1 Sub Main()

    Dim obj As Object = "asdfsdasdfsadfasdfasdfasdfsdasdfsadfasdfasdfdafsdfasd"
    Dim LL As New List(Of String), SWW As System.Diagnostics.Stopwatch

    LL.Clear()

    SWW = Stopwatch.StartNew()
    For i = 0 To Short.MaxValue
        LL.Add(obj.ToString)
    Next
    Console.WriteLine("obj.ToString took {0}.", SWW.ElapsedTicks)

    LL.Clear()
    SWW = Stopwatch.StartNew()
    For i = 0 To Short.MaxValue
        LL.Add(CStr(obj))
    Next
    Console.WriteLine("CStr(obj) took {0}.", SWW.ElapsedTicks)

    LL.Clear()
    SWW = Stopwatch.StartNew()
    For i = 0 To Short.MaxValue
        LL.Add(DirectCast(obj, String))
    Next
    Console.WriteLine("DirectCast(obj, String) took {0}.", SWW.ElapsedTicks)





    Console.WriteLine("---------------- Integer To String ----------- ")

    obj = 15522

    LL.Clear()
    SWW = Stopwatch.StartNew()
    For i = 0 To Short.MaxValue
        LL.Add(obj.ToString)
    Next
    Console.WriteLine("obj.ToString took {0}.", SWW.ElapsedTicks)

    LL.Clear()
    SWW = Stopwatch.StartNew()
    For i = 0 To Short.MaxValue
        LL.Add(CStr(obj))
    Next
    Console.WriteLine("CStr(obj) took {0}.", SWW.ElapsedTicks)

    LL.Clear()
    SWW = Stopwatch.StartNew()
    For i = 0 To Short.MaxValue
        Dim str As String = TryCast(obj, String)
        ' This obviously fails, as obj is not a string, which is why it is so fast.. str is then nothing
        LL.Add(str)

    Next
    Console.WriteLine("DirectCast(obj, String) took {0}.", SWW.ElapsedTicks)

    Console.Read()
End Sub

End Module

My results:

(string ) : ToString : 2288; CStr: 2275; DirectCast: 1586

(integer) : ToString : 10526; CStr: 13184; TryCast: 982

Tags:

Vb.Net

Casting