Click here to Skip to main content
15,346,154 members
Articles / Programming Languages / Visual Basic
Posted 8 Apr 2007


35 bookmarked

DirectCast vs. CType

Rate me:
Please Sign up or sign in to vote.
4.30/5 (31 votes)
8 Apr 2007CPOL2 min read
When to use DirectCast vs. CType.


When should you use CType and when should you use DirectCast and which one performs better? The short answer is: DirectCast is twice as fast for value types (integers, etc.), but identical for reference types.


The first thing to understand is that CType and DirectCast are not the same thing. Only CType can convert the underlying object to a new instance of an object of a different type. For example, you want to turn an integer into a string. Since an integer doesn't inherit a string, a new instance of a String object must be created in order to store the number as a string. CType can do this, DirectCast cannot. Note: There are other ways to do this too such as the Convert.ToString method or CStr().

Dim MyInt As Integer = 123
Dim MyString1 As String = CType(MyInt, String)
Dim MyString2 As String = DirectCast(MyInt, String) ' This will not work

What DirectCast and CType do have in common is their ability to convert an object to a new type based on inheritance or implementation. For example, if you have a string but it is stored in a variable of type Object, you can use DirectCast or CType to treat that variable as an object of type String because type String inherits type Object. In this case, the underlying data in memory is not actually changing, nor is any processing happening on that data.

Dim MyObject As Object = "Hello World"
Dim MyString1 As String = CType(MyObject, String)
Dim MyString2 As String = DirectCast(MyObject, String) ' This will work

The danger: You must know what type you are dealing with before using DirectCast. If you have a variable of type Object and you use DirectCast to treat it as a String, you'd better be sure that variable actually contains a String (or Nothing). If an integer somehow found its way into that variable, an exception will be thrown.

A way to check in code if DirectCast will work is by using the TypeOf operator:

If TypeOf MyObject Is String Then

So, assuming you are doing a conversion based on inheritance or implementation, you have a choice: DirectCast vs. CType. Which is better?

The Answer

DirectCast. According to the .NET documentation: DirectCast does not use the Visual Basic run-time helper routines for conversion, so it can provide somewhat better performance than CType.

The included project runs a performance test on each scenario: DirectCast vs. CType for value and reference types. Remember, reference types are types like Forms, Controls, Strings, and custom classes, whereas Value types are types like integers, doubles, and custom structures. Here are the results; the numbers are in milliseconds for 1 million iterations:

DirectCast on a reference type:


CType on a reference type:


DirectCast on a value type:


CType on a value type:


The verdict: DirectCast out performs CType for value types almost 2 to 1. DirectCast also out performs CType for reference types albeit by a much slimmer margin.

Note: Subsequent tests produced very similar results; try for yourself.

Another reason to use DirectCast is that it's not VB.NET specific and is therefore more portable to other languages. So C# and other programmers might have an easier time reading their code if they encountered DirectCast.


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


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.

Comments and Discussions

QuestionMy vote of 4 Pin
User 418025410-Aug-15 10:27
MemberUser 418025410-Aug-15 10:27 
GeneralMy vote of 4 Pin
pldakob31-Jan-13 4:04
Memberpldakob31-Jan-13 4:04 
GeneralMy vote of 5 Pin
Harry von Borstel17-Dec-12 22:16
MemberHarry von Borstel17-Dec-12 22:16 
GeneralMy vote of 4 Pin
lloyd_100630-Sep-12 20:03
Memberlloyd_100630-Sep-12 20:03 
GeneralMy vote of 5 Pin
Robert J. Good4-Sep-12 12:47
professionalRobert J. Good4-Sep-12 12:47 
GeneralMy vote of 5 Pin
Lisa Z. Morgan11-Feb-11 6:30
MemberLisa Z. Morgan11-Feb-11 6:30 
GeneralA better "safe" means of using DirectCast Pin
Yoooder12-Mar-09 10:23
MemberYoooder12-Mar-09 10:23 
GeneralRe: A better "safe" means of using DirectCast Pin
AspDotNetDev8-Jun-10 8:54
protectorAspDotNetDev8-Jun-10 8:54 
QuestionOverhead Pin
stixoffire9-Apr-07 15:13
Memberstixoffire9-Apr-07 15:13 
AnswerRe: Overhead Pin
Tim Greenfield16-Apr-07 18:09
MemberTim Greenfield16-Apr-07 18:09 
GeneralRe: Overhead Pin
stixoffire16-Apr-07 20:51
Memberstixoffire16-Apr-07 20:51 
GeneralAnother reason to use DirectCast... Pin
Steven Campbell9-Apr-07 7:40
MemberSteven Campbell9-Apr-07 7:40 
Another good reason to use DirectCast is readability...

If I read code, then DirectCast indicates that the programmer knew that the type was correct, and she only needed to cast it to make it easier to read. It is a much more "intentional" way of coding than CType.

For similar reasons, I turn on Option Strict on most VB.NET projects - forcing myself to cast variables uncovers many bugs and design smells.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.