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.
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.
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.
- Difference between NameSpace and Assembly
- Execution process for managed code
- What is an Asssembly Qualified Name
- How to Assembly versioning
- Add and remove an assembly from GAC
- What is Native Image Generator (Ngen.exe)?
- How to force Garbage Collection
- Obsolete or Deprecated in NET Framework
- What is lazy initialization
- Difference between Clone and Copy
- Difference between Finalize() and Dispose()
- Difference between static and constant