VB.NET 中 DirectCast() 和 CType() 的区别 [英] Difference between DirectCast() and CType() in VB.NET

查看:37
本文介绍了VB.NET 中 DirectCast() 和 CType() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一位经验丰富的 C/C++/C# 程序员,刚刚接触 VB.NET.我通常使用 CType(和 CInt、CBool​​、CStr)进行转换,因为它的字符较少,并且是我接触到的第一种转换方式,但我也知道 DirectCast 和 TryCast.

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.

简单地说,DirectCast 和 CType 之间有什么区别(转换效果、性能等)吗?我理解 TryCast 的想法.

Simply, are there any differences (effect of cast, performance, etc.) between DirectCast and CType? I understand the idea of TryCast.

推荐答案

首先要注意的是 VB.NET 没有直接模拟 C# 的 (type)instance 转换机制.我提出这个是因为它作为比较两个 VB.NET 运算符(它们是运算符,不是函数,即使它们具有函数语义)的起点和共同参考很有用.

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 and common reference in comparing the two VB.NET operators (and they are operators, not functions, even though they have function semantics).

DirectCast() 比 C# 转换运算符更严格.它只允许你在被投射的项目已经是你要投射到的类型时进行投射.我相信它仍然会取消装箱值类型,但否则它不会进行任何转换.因此,例如,您不能从 short 转换为 int,就像使用 C# (int) 转换一样.但是,如果您的底层 IEnumerable 对象变量确实是一个 Array,您可以从 IEnumerable 转换为数组.当然,您可以从 Object 转换为任何类型,假设您的对象实例的类型确实在继承树中的某个位置低于您的转换类型.

DirectCast() is more strict than the C# casting operator. It only allows you to cast when the item being cast already is the 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.

这是可取的,因为它更快.需要进行的转换和类型检查较少.

This is desirable because it's much faster. There's less conversion and type checking that needs to take place.

CType() 不如 C# 转换运算符严格.它会做一些你不能用简单的 (int) 风格的转换做的事情,比如将字符串转换为整数.它与在 C# 中调用 Convert.To___() 一样强大,其中 ___ 是您的演员表的目标类型.

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.

这是可取的,因为它非常强大.然而,这种能力是以牺牲性能为代价的.它不如 DirectCast() 或 C# 的转换运算符快,因为它可能需要做很多工作才能完成转换.通常,您应该尽可能选择 DirectCast().

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.

最后,您错过了一个转换运算符:TryCast(),它直接类似于 C# 的 as 运算符.

Finally, you missed one casting operator: TryCast(), which is a direct analog to C#'s as operator.

这篇关于VB.NET 中 DirectCast() 和 CType() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆