How should I cast in VB.NET?

前端 未结 7 531
再見小時候
再見小時候 2020-11-29 17:29

Are all of these equal? Under what circumstances should I choose each over the others?

  • var.ToString()

  • CStr(var)

  • CType(var,

相关标签:
7条回答
  • 2020-11-29 18:01

    Those are all slightly different, and generally have an acceptable usage.

    • var.ToString() is going to give you the string representation of an object, regardless of what type it is. Use this if var is not a string already.
    • CStr(var) is the VB string cast operator. I'm not a VB guy, so I would suggest avoiding it, but it's not really going to hurt anything. I think it is basically the same as CType.
    • CType(var, String) will convert the given type into a string, using any provided conversion operators.
    • DirectCast(var, String) is used to up-cast an object into a string. If you know that an object variable is, in fact, a string, use this. This is the same as (string)var in C#.
    • TryCast (as mentioned by @NotMyself) is like DirectCast, but it will return Nothing if the variable can't be converted into a string, rather than throwing an exception. This is the same as var as string in C#. The TryCast page on MSDN has a good comparison, too.
    0 讨论(0)
  • 2020-11-29 18:01

    At one time, I remember seeing the MSDN library state to use CStr() because it was faster. I do not know if this is true though.

    0 讨论(0)
  • 2020-11-29 18:05

    MSDN seems to indicate that the Cxxx casts for specific types can improve performance in VB .NET because they are converted to inline code. For some reason, it also suggests DirectCast as opposed to CType in certain cases (the documentations states it's when there's an inheritance relationship; I believe this means the sanity of the cast is checked at compile time and optimizations can be applied whereas CType always uses the VB runtime.)

    When I'm writing VB .NET code, what I use depends on what I'm doing. If it's prototype code I'm going to throw away, I use whatever I happen to type. If it's code I'm serious about, I try to use a Cxxx cast. If one doesn't exist, I use DirectCast if I have a reasonable belief that there's an inheritance relationship. If it's a situation where I have no idea if the cast should succeed (user input -> integers, for example), then I use TryCast so as to do something more friendly than toss an exception at the user.

    One thing I can't shake is I tend to use ToString instead of CStr but supposedly Cstr is faster.

    0 讨论(0)
  • 2020-11-29 18:16

    User Konrad Rudolph advocates for DirectCast() in Stack Overflow question "Hidden Features of VB.NET".

    0 讨论(0)
  • 2020-11-29 18:20

    I prefer the following syntax:

    Dim number As Integer = 1
    Dim str As String = String.TryCast(number)
    
    If str IsNot Nothing Then
    

    Hah you can tell I typically write code in C#. 8)

    The reason I prefer TryCast is you do not have to mess with the overhead of casting exceptions. Your cast either succeeds or your variable is initialized to null and you deal with that accordingly.

    0 讨论(0)
  • 2020-11-29 18:20

    According to the certification exam you should use Convert.ToXXX() whenever possible for simple conversions because it optimizes performance better than CXXX conversions.

    0 讨论(0)
提交回复
热议问题