Monday, July 16, 2007

DirectCast vs CType

  • 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.

No comments: