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

查看:239
本文介绍了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 in comparing the two VB.NET operators (and they are operators, not functions, even though they have function semantics).

DirectCast() 比C#强制转换运算符更严格.仅当您正在投射的项目您要投射的类型时,它才允许您进行投射.我相信它将仍然取消对值类型的装箱,但是否则它将不会进行任何转换.因此,例如,您不能像使用C#(int)强制转换那样从short强制转换为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天全站免登陆