SiteMap  | About    

Difference between CType and DirectCast

CType or DirectCast

CType and DirectCast take an expression to be converted as the first argument, and the type to convert it to as the second argument.


CType Function returns the result of explicitly converting an expression to a specific data type, object, structure, class, or interface.

CType(expression, typename)

DirectCast does not use the Visual Basic run-time helper routines for conversion, so it can provide somewhat better performance than CType when converting to and from data type Object.

DirectCast(expression, typename)

Both conversions fail if there is no conversion defined between the data type of the expression and he data type specified as the second argument.

DirectCast() is more strict than the C# casting operator. It only allows you to cast when the item being cast already is the type you are casting to. It won't do any conversion. Use DirectCast if you're absolutely positively sure that an object is the specified type and the run-time type of the expression are the same. If you're not sure but expect that the conversion will work, use CType. (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.