Hey frnds, I know about Ctype??
Please forward me what is Directcast,Cz i hace faced this question in Interview.Difference B/w the aboce two???/

8 Years
Discussion Span
Last Post by Jx_Man

When should you use CType and when should you use DirectCast and which one preforms 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, if 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; 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 preforms 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#...etc programmers might have an easier time reading your code if they encountered DirectCast.

For more clearly visit this site

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.