VB.NET 中 DirectCast() 和 CType() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3056514/
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
Difference between DirectCast() and CType() in VB.NET
提问by Caleb Hearth
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 fewer characters and was the first way of casting which I was exposed to, but I am aware of DirectCast and TryCast as well.
我是一位经验丰富的 C/C++/C# 程序员,刚刚接触 VB.NET。我通常使用 CType(和 CInt、CBool、CStr)进行转换,因为它的字符较少,并且是我接触到的第一种转换方式,但我也知道 DirectCast 和 TryCast。
Simply, are there any differences (effect of cast, performance, etc.) between DirectCast and CType? I understand the idea of TryCast.
简单地说,DirectCast 和 CType 之间有什么区别(转换效果、性能等)?我理解 TryCast 的想法。
回答by Joel Coehoorn
The first thing to note is VB.NET does not have a direct analog to C#'s (type)instance
casting mechanism. I bring this up because it's useful as a starting point in comparing the two VB.NET operators (and they are operators, not functions, even though they have function semantics).
首先要注意的是,VB.NET 没有直接模拟 C# 的(type)instance
转换机制。我提出这个是因为它作为比较两个 VB.NET 运算符(它们是运算符,而不是函数,即使它们具有函数语义)的起点很有用。
DirectCast()
is more strict than the C# casting operator. It only allows you to cast when the item being cast already isthe type you are casting to. I believe it will still unbox value types, but otherwise it won't do any conversion. So, for example, you can't cast from short
to int
, like you could with a C# (int)
cast. But you can cast from an IEnumerable
to an array, if your underlying IEnumerable
object variable really is an Array
. And of course you can cast from Object
to anything, assuming the type of your object instance really is somewhere below your cast type in the inheritance tree.
DirectCast()
比 C# 转换运算符更严格。它只允许您在被转换的项目已经是您要转换到的类型时进行转换。我相信它仍然会取消装箱值类型,但否则它不会进行任何转换。因此,例如,您不能像使用 C# 转换那样从short
to 转换。但是,如果您的底层对象变量确实是一个. 当然,您可以从任何类型转换为任何类型,假设您的对象实例的类型确实在继承树中的类型低于您的转换类型。int
(int)
IEnumerable
IEnumerable
Array
Object
This is desirable because that makes it faster. There's a little less conversion and type checking that needs to take place.
这是可取的,因为这使它更快。需要进行的转换和类型检查要少一些。
CType()
is less strict than the C# casting operator. It will do things you just can't do with a simple (int)
-style cast, like convert a string to an integer. It has as much power as calling Convert.To___()
in C#, where the ___
is the target type of your cast.
CType()
不如 C# 强制转换运算符严格。它会做一些你不能用简单的(int)
风格转换做的事情,比如将字符串转换为整数。它与Convert.To___()
在 C# 中调用一样强大,其中___
是您的演员表的目标类型。
This is desirable because it's very powerful. However, this power comes at the cost of performance; it's not as fast as DirectCast()
or C#'s cast operator because it might need to do quite a lot of work to finish the cast. Generally you should prefer DirectCast()
when you can.
这是可取的,因为它非常强大。然而,这种能力是以牺牲性能为代价的。它不如DirectCast()
C# 的转换运算符快,因为它可能需要做很多工作才能完成转换。一般来说,你应该选择DirectCast()
什么时候可以。
Finally, you missed one casting operator: TryCast()
, which is a direct analog to C#'s as
operator.
最后,您错过了一个强制转换运算符: TryCast()
,它直接类似于 C# 的as
运算符。
回答by Abhay
With CType
you can write some thing like this Ctype("string",Integer)
. But with DirectCast
the above statement would give compile time error.
随着CType
你可以写一些像这样的事情Ctype("string",Integer)
。但是用DirectCast
上面的语句会给出编译时错误。
Dim a As Integer = DirectCast("1", Integer) 'Gives compiler error
Dim b As Integer = CType("1", Integer) 'Will compile
回答by Hemantha
DirectCast
is more restrictive than CType
.
DirectCast
比 更严格CType
。
For example, this will throw an error:
例如,这将引发错误:
Sub Main()
Dim newint As Integer = DirectCast(3345.34, Integer)
Console.WriteLine(newint)
Console.ReadLine()
End Sub
It will also be shown in the Visual Studio IDE.
它也将显示在 Visual Studio IDE 中。
This however, does not throw an error:
但是,这不会引发错误:
Sub Main()
Dim newint As Integer = CType(3345.34, Integer)
Console.WriteLine(newint)
Console.ReadLine()
End Sub