我应该如何在 VB.NET 中进行转换?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:42:14  来源:igfitidea点击:

How should I cast in VB.NET?

.netvb.netcasting

提问by Zack Peterson

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

所有这些都是平等的吗?在什么情况下我应该选择一个而不是其他的?

  • var.ToString()

  • CStr(var)

  • CType(var, String)

  • DirectCast(var, String)

  • var.ToString()

  • CStr(var)

  • CType(var, String)

  • DirectCast(var, String)



EDIT: Suggestion from NotMyself

编辑:来自NotMyself 的建议……

  • TryCast(var, String)
  • TryCast(var, String)

回答by bdukes

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 varis 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)varin C#.
  • TryCast(as mentioned by @NotMyself) is like DirectCast, but it will return Nothingif the variable can't be converted into a string, rather than throwing an exception. This is the same as var as stringin C#. The TryCastpage on MSDN has a good comparison, too.
  • var.ToString()将给你一个对象的字符串表示,不管它是什么类型。如果var不是字符串,则使用它。
  • CStr(var)是 VB 字符串转换运算符。我不是一个 VB 人,所以我建议避免它,但它不会真正伤害任何东西。我认为它与CType.
  • CType(var, String)将使用任何提供的转换运算符将给定类型转换为字符串。
  • DirectCast(var, String)用于将对象向上转换为字符串。如果您知道对象变量实际上是一个字符串,请使用 this。这与(string)varC# 中的相同。
  • TryCast(如@NotMyself 所述)就像DirectCast,但Nothing如果变量无法转换为字符串,它将返回,而不是抛出异常。这与var as stringC# 中的相同。TryCastMSDN 上的页面也有很好的比较。

回答by Zack Peterson

Cstr()is compiled inline for better performance.

Cstr()内联编译以获得更好的性能。

CTypeallows for casts between types if a conversion operator is defined

CType如果定义了转换运算符,则允许在类型之间进行强制转换

ToString()Between base type and string throws an exception if conversion is not possible.

ToString()如果无法转换,则基类型和字符串之间会引发异常。

TryParse()From String to base typeifpossible otherwise returns false

TryParse()从 String 到 basetypeif可能否则返回 false

DirectCastused if the types are related via inheritance or share a common interface , will throw an exception if the cast is not possible, trycastwill return nothing in this instance

DirectCast如果类型通过继承相关或共享一个公共接口,则使用,如果无法转换,trycast将抛出异常,在此实例中将不返回任何内容

回答by NotMyself

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)

哈哈,你可以说我通常用 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.

我更喜欢 TryCast 的原因是您不必处理投射异常的开销。您的转换要么成功,要么您的变量被初始化为 null,然后您相应地进行处理。

回答by OwenP

MSDNseems 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 DirectCastas 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.)

MSDN似乎表明特定类型的 Cxxx 强制转换可以提高 VB .NET 中的性能,因为它们被转换为内联代码。出于某种原因,它还建议在某些情况下使用DirectCast而不是 CType(文档说明它是在存在继承关系时;我相信这意味着在编译时检查转换的完整性并且可以应用优化,而 CType 总是使用VB 运行时。)

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.

当我编写 VB .NET 代码时,我使用什么取决于我在做什么。如果它是我要扔掉的原型代码,我会使用我碰巧输入的任何东西。如果是我认真对待的代码,我会尝试使用 Cxxx 演员表。如果不存在,如果我有理由相信存在继承关系,我将使用 DirectCast。如果在这种情况下我不知道强制转换是否应该成功(例如,用户输入 -> 整数),那么我使用 TryCast 来做一些比向用户抛出异常更友好的事情。

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

我不能动摇的一件事是我倾向于使用 ToString 而不是 CStr 但据说 Cstr 更快。

回答by SickPuP

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

根据认证考试,您应该尽可能使用 Convert.ToXXX() 进行简单的转换,因为它比 CXXX 转换能更好地优化性能。

回答by joek1975

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.

有一次,我记得看到 MSDN 库状态使用 CStr() 因为它更快。我不知道这是否属实。