VB CStr, CDate, CBool, etc. vs. DirectCast for casting without conversion

前端 未结 3 1394
走了就别回头了
走了就别回头了 2021-01-06 04:56

I usually avoid VB\'s built-in conversion functions (CStr, CDate, CBool, CInt, etc.) unless I need to do an actual conversion. If I\'m just casting, say from an object to a

3条回答
  •  轮回少年
    2021-01-06 05:55

    This is a good post with discussion in the comments about DirectCast versus the CType casts and variations.

    In short, if you want to be explicit about it and know what to expect, DirectCast is suggested. On the other hand, a comment by Paul Vick (VB Technical Lead) says it doesn't matter much and to just use the CType variations.

    Some useful links gleaned from that post:

    • How should I cast in VB.NET?
    • DirectCast Revealed (post on Paul Vick's blog)

提交回复
热议问题