Click here to Skip to main content
12,447,227 members (52,610 online)
Click here to Skip to main content
Articles » Languages » VB.NET » General » Downloads

Stats

159.9K views
35 bookmarked
Posted

DirectCast vs. CType

, 8 Apr 2007 CPOL
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)

Share

About the Author

Tim Greenfield
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.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160811.3 | Last Updated 8 Apr 2007
Article Copyright 2007 by Tim Greenfield
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid