Alex Rivera | Logout

Casting DataTypes with DirectCast, CType, TryCast

Asked 2010-04-24T07:14:50.857
42

Ever since I moved from VB6 to VB.NET somewhere in 2005, I've been using CType to do casting from one data type to another. I do this because it is simply faster to type, used to exist in VB6 and I do not know why I have to be using DirectCast if there is apparently no difference between them.

I use TryCast once in a while because I understand that sometimes casting can fail. I however cannot get the difference between CType and DirectCast.

Can anyone tell me the difference in plain simple English what the difference the two (CType and DirectCast)? Adding examples of where to use what as well would be helpful.

Edit
Report

1 Answer

17

DirectCast is twice as fast for value types (integers...etc), but identical for reference types.

For more information see the "Conversion Functions, CType, DirectCast, and System.Convert" section on this MSDN page.

answered 2010-04-24T07:23:58.960

Your Answer