[英]How should I cast in VB.NET?
Are all of these equal? 所有这些都是平等的吗? Under what circumstances should I choose each over the others?
我应该在什么情况下选择其他人?
var.ToString() var.ToString()
CStr(var) CStr的(VAR)
CType(var, String) CType(var,String)
DirectCast(var, String) DirectCast(var,String)
EDIT: Suggestion from NotMyself … 编辑:来自NotMyself的建议......
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. ToString
()
将为您提供对象的字符串表示形式,无论它是什么类型。 Use this if var
is not a string already. var
不是字符串,请使用此选项。 CStr
(var)
is the VB string cast operator. CStr
(var)
是VB字符串转换运算符。 I'm not a VB guy, so I would suggest avoiding it, but it's not really going to hurt anything. CType
. CType
基本相同。 CType
(var, String)
will convert the given type into a string, using any provided conversion operators. CType
(var, String)
将使用任何提供的转换运算符将给定类型转换为字符串。 DirectCast
(var, String)
is used to up-cast an object into a string. DirectCast
(var, String)
用于将对象上传到字符串中。 If you know that an object variable is, in fact, a string, use this. (string)var
in C#. (string)var
相同。 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. TryCast
(如@ NotMyself所述 )与DirectCast
类似,但如果变量无法转换为字符串,则返回Nothing
,而不是抛出异常。 This is the same as var as string
in C#. var as string
相同。 The TryCast
page on MSDN has a good comparison, too. TryCast
页面也有很好的比较。 Cstr()
is compiled inline for better performance. Cstr()
是内联编译的,以获得更好的性能。
CType
allows 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 typeif
possible otherwise returns false TryParse()
从String到base typeif
否则返回false
DirectCast
used if the types are related via inheritance or share a common interface , will throw an exception if the cast is not possible, trycast
will return nothing in this instance 如果类型通过继承相关或共享一个公共接口,则使用
DirectCast
,如果无法进行trycast
将抛出异常, trycast
将在此实例中不返回任何内容
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#. 哈,你可以告诉我通常用C#编写代码。 8)
8)
The reason I prefer TryCast is you do not have to mess with the overhead of casting exceptions. 我更喜欢TryCast的原因是你不必乱用于抛出异常的开销。 Your cast either succeeds or your variable is initialized to null and you deal with that accordingly.
你的演员要么成功,要么你的变量被初始化为null,你会相应地处理它。
MSDN seems to indicate that the Cxxx casts for specific types can improve performance in VB .NET because they are converted to inline code. MSDN似乎表明,针对特定类型的Cxxx强制转换可以提高VB .NET的性能,因为它们被转换为内联代码。 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.)
出于某种原因,它还建议DirectCast而不是CType在某些情况下(文档说明它是在存在继承关系的时候;我相信这意味着在编译时检查强制转换并且可以应用优化,而CType总是使用VB运行时。)
When I'm writing VB .NET code, what I use depends on what I'm doing. 当我编写VB .NET代码时,我使用的内容取决于我正在做什么。 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.
如果是我认真的代码,我会尝试使用Cxxx演员。 If one doesn't exist, I use DirectCast if I have a reasonable belief that there's an inheritance relationship.
如果一个不存在,我会使用DirectCast,如果我有一个合理的信念,即存在继承关系。 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.
如果这是我不知道演员表是否应该成功的情况(例如用户输入 - >整数),那么我使用TryCast来做一些比在用户处抛出异常更友好的事情。
One thing I can't shake is I tend to use ToString instead of CStr but supposedly Cstr is faster. 我无法理解的一件事是我倾向于使用ToString而不是CStr,但据说Cstr更快。
根据认证考试,您应尽可能使用Convert.ToXXX()进行简单转换,因为它优于CXXX转换优化性能。
At one time, I remember seeing the MSDN library state to use CStr() because it was faster. 有一次,我记得看到MSDN库状态使用CStr()因为它更快。 I do not know if this is true though.
我不知道这是否属实。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.