views:

244

answers:

1

I am an experienced C/C++/C# programmer who has just gotten into VB.NET. I generally use CType (and CInt, CBool, CStr) for casts because it is less characters and was the first way of casting which I was exposed to, but I am aware of DirectCast and TryCast as well.

Simply, are there any differences (effect of cast, performance, etc.) between DirectCast and CType? I understand the idea of TryCast.

+8  A: 
Joel Coehoorn
Great answer. Thank you very much.
Caleb Thompson
+1 I'd say the strictness of `DirectCast` is another advantage. If you make a mistake the compiler tells you immediately, but with `CType` a mistake just might cause occasional wrong behaviour at run-time - maybe on some user machine with different regional settings.
MarkJ
Great answer. So in order of complexity (small to large): `DirectCast`, `TryCast`, `CType`/`Convert.ToXYZ()`, `C<xyz>()` would be correct?
motto
@motto - close. The C<xyz>() "functions" should be moved higher on the list, as they really are operators rather than functions, even though they have function semantics. For those types that have them, they are very close to C#'s (type)casting, but will do just a little more work.
Joel Coehoorn