Difference between DirectCast and CType

Posted on 2005-04-18
Last Modified: 2011-10-03

If I want to resume the differences, as explained in the MSDN:
- CType can be used for narrowing or widening conversion
- to use DirectCast, there must be an heritance or implementation relation between the classes
- DirectCast could be quicker because it doesn't use the Visual Basic run-time helper routines for conversion.

First question : what is the "Visual Basic run-time helper routines for conversion" ?
Second question : in the version 1.0 and 1.1 (the help has been modified in 2.0) it was specified that the dynamic type of the two objects must be identical to have DirectCast succeeds, whereas it is only specified a relation in inheritance or implementation in 2.0. But if you look to the generated MSIL code (I have looked in 2.0), you will notice that both classes are compiled using the directive "castclass"
So I wonder how one can be quicker that the second one if they are compiled in the identical code ?

Question by:Jarodtweiss
    LVL 28

    Expert Comment

    Dim Q As Object = 2.37   ' Requires Option Strict to be Off.
    Dim I As Integer = CType(Q, Integer)   ' Succeeds.
    Dim J As Integer = DirectCast(Q, Integer)   ' Fails.

    Both keywords take an expression to be converted as the first argument, and the type to convert it to as
    the second argument. Both conversions fail if there is no conversion defined between the data type of
    the expression and the data type specified as the second argument.

    The difference between the two keywords is that CType succeeds as long as there is a valid conversion
    defined between the expression and the type, whereas DirectCast requires the run-time type of an object
    variable to be the same as the specified type. If the specified type and the run-time type of the expression
    are the same, however, the run-time performance of DirectCast is better than that of CType.

    In the preceding example, the run-time type of Q is Double. CType succeeds because Double can be
    converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer.
    LVL 4

    Author Comment


    thanks for your explanation.
    However, it is not really what I was asking for. I have also explained this difference in my question (with less precise words, I agree).
    What I was asking is how can we a better run time performance with DirectCast when the two casts are compiled in MSIL using the same keyword "castclass".
    I have checked in the ECMA documentation abot this keyword but nothing special is said. No extra metadata is generated, nothing that can let us think that the two casts will behave differently.
    I have done some performance measuremenent and there is a difference. DirectCast is sometimes quicker, sometimes not, depending of the complixiy of the class. But how can this be possible if the two casts are compiled the same way ? They should be executed the same way !
    LVL 28

    Expert Comment

    CType slower than DirectCast

    Use the DirectCast keyword instead of the CType keyword when you're just
    casting an object variable to a variable of a different type in .NET.
    DirectCast can deliver better performance because it never calls methods in the
    Microsoft.VisualBasic DLL. You should notice the risk of using DirectCast,
    though. DirectCast can raise a run-time error if the run-time types don't
    match. In this case, CType may perform type coercion, while DirectCast raises
    an error.

    LVL 4

    Author Comment

    You say "DirectCast can deliver better performance because it never calls methods in the Microsoft.VisualBasic DLL"
    I agree with you, it is probably what MS means when they said that it doesn't use the Visual Basic run-time helper routines for conversion

    And I can imagine that extra checks are performed during the compilation into MSIL. But what is executed by the CLR ? The code compiled by the JIT (or NGEN) from the MSIL.
    Sorry if I insist on that point but let's take another example and the differences between Convert.ToInt32 and Conversions.ToInteger (introduced in .NET FX 2.0). In that case, the two methods are converted to something different in MSIL. So I do not  have any problem to admit than the Conversions class is doing something else (even if I assume that it is doing a simple delegation to provide to the VB developpers the ability to work with VB types instead of CLR types)

    To sum up, how can the same MSIL code (produced either by CType or DirectCast) be executed differently ?

    Accepted Solution

    Question Closed, 100 points refunded.
    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    ClickOnce Install - Shortcut Question 3 40
    Session variables expiration ASP.NET C# 3 37
    Help file 1 28
    Aspnet xml 2 27
    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now