Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / Visual Basic

DirectCast vs. CType

Rate me:
Please Sign up or sign in to vote.
4.30/5 (31 votes)
8 Apr 2007CPOL2 min read 213.4K   35  
When to use DirectCast vs. CType.
Public Class Form1
    Private Sub ButtonCTypeR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCTypeR.Click
        Debug.WriteLine("CType on a reference type")
        Dim StartTime As Date = Date.Now
        For i As Integer = 0 To 1000000
            Dim MyObject As Object = "Hello World"
            Dim MyString As String = CType(MyObject, String)
        Next
        Debug.WriteLine(Date.Now.Subtract(StartTime).TotalMilliseconds)
    End Sub

    Private Sub ButtonDirectCastR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDirectCastR.Click
        Debug.WriteLine("DirectCast on a reference type")
        Dim StartTime As Date = Date.Now
        For i As Integer = 0 To 1000000
            Dim MyObject As Object = "Hello World"
            Dim MyString As String = DirectCast(MyObject, String)
        Next
        Debug.WriteLine(Date.Now.Subtract(StartTime).TotalMilliseconds)
    End Sub

    Private Sub ButtonCTypeV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCTypeV.Click
        Debug.WriteLine("CType on a value type")
        Dim StartTime As Date = Date.Now
        For i As Integer = 0 To 1000000
            Dim MyObject As Object = 123
            Dim MyInt As Integer = CType(MyObject, Integer)
        Next
        Debug.WriteLine(Date.Now.Subtract(StartTime).TotalMilliseconds)
    End Sub

    Private Sub ButtonDirectCastV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDirectCastV.Click
        Debug.WriteLine("DirectCast on a value type")
        Dim StartTime As Date = Date.Now
        For i As Integer = 0 To 1000000
            Dim MyObject As Object = 123
            Dim MyInt As Integer = DirectCast(MyObject, Integer)
        Next
        Debug.WriteLine(Date.Now.Subtract(StartTime).TotalMilliseconds)
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am a software architect and engineer who has been working in the industry since 1994. Although I officially started writing software many years earlier at age 11 by authoring games, graphics demos, or whatever I found interesting. I even landed my first consulting job at age 16 implementing a newly invented mathimatical algorithm for a team of researchers at the local University.

I've designed and implemented hundreds if not thousands of applications: client, web, and hybrid of all scales and was a contributing author to the Black Belt programming column of Visual Basic Programmer's Journal.

My goals when coding are always to write clean, reusable and optimized code using the latest and technology, tools and techniques.

Comments and Discussions