在VB.Net和C#中进行转换 [英] Casting in VB.Net and C#

查看:75
本文介绍了在VB.Net和C#中进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对.Net相对较新,但多年来一直使用VB和C / C ++。


VB6及其早期的一个缺点是难以投射

'''记录'到不同的''形状''所以可以对它进行不同的操作

。例如,我有一个复杂的数据结构,我可以在VB6 TYPE声明中表示

,但我不能轻易地将其转换为固定的

长度的无符号字节数组这样我就可以对内容进行校验和

计算。另一种情况是从

文件中读取缓冲区,然后根据某些标题

信息解释内容。这在VB6中也非常困难。


VB.Met和C#中有哪些设施(没有

指针,这使得这很容易虽然容易出错,但在C和C ++中)?


-Ken

I am relatively new to .Net, but have been using VB and C/C++ for years.

One of the drawbacks with VB6 and earlier was the difficulty in casting a
''record'' to a different ''shape'' so one could perform different manipulations
on it. For example, I have a complex data structure, which I can represent
in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
length array of unsigned bytes so that I could perform a checksum
calculation on the contents. Another case is where a read a buffer from a
file and then wish to interpret the contents based on some header
information. This was also very difficult in VB6.

What facilities are available in VB.Met and C# (which does not have
pointers, which made this so easy, albeit error prone, in C and C++)?

-Ken

推荐答案

在VB.NET中您可以使用:

- CType

- DirectCast


在C#中你可以使用:

- (typeName)


-

Greetz


Jan Tielens

________________________________

阅读我的博客: http://weblogs.asp.net/ jan

" Ken Allen" <柯****** @ sympatico.ca>在消息中写道

新闻:ee ************** @ tk2msftngp13.phx.gbl ...
In VB.NET you can use:
- CType
- DirectCast

In C# you can use:
- (typeName)

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"Ken Allen" <ke******@sympatico.ca> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
我比较新.Net,但多年来一直在使用VB和C / C ++。

VB6及其早期的一个缺点就是难以将
记录转换为不同的 'shape''因此可以对它进行不同的
操作。例如,我有一个复杂的数据结构,我可以在VB6 TYPE声明中表示,但我不能轻易地将其转换为固定的长度无符号字节数组,以便我可以执行校验和
计算内容。另一种情况是从
文件中读取缓冲区然后希望根据某些标题信息解释内容。这在VB6中也非常困难。

VB.Met和C#中有哪些功能可用(没有指针,这使得这很容易,尽管容易出错,在C和C ++)?

-Ken
I am relatively new to .Net, but have been using VB and C/C++ for years.

One of the drawbacks with VB6 and earlier was the difficulty in casting a
''record'' to a different ''shape'' so one could perform different manipulations on it. For example, I have a complex data structure, which I can represent
in a VB6 TYPE declaration, but I cannot easily convert that to a fixed
length array of unsigned bytes so that I could perform a checksum
calculation on the contents. Another case is where a read a buffer from a
file and then wish to interpret the contents based on some header
information. This was also very difficult in VB6.

What facilities are available in VB.Met and C# (which does not have
pointers, which made this so easy, albeit error prone, in C and C++)?

-Ken



只是为了澄清CType和DirectCast之间的区别。


DirectCast只能用于参考类型

,它们必须是同一类型。然而,CType

可用于价值类型和参考类型

,其中铸造是允许的。


DirectCast更快在运行时比CType。


问候 - OHM#

Jan Tielens写道:
Just to clarify the difference between CType and DirectCast.

DirectCast can only be used on reference types
and they have to be the same type. Whereas, CType
can be used on both Value Types and Reference Types
where the casting is permissable.

DirectCast is faster at runtime than CType.

Regards - OHM#
Jan Tielens wrote:
在VB.NET中你可以使用:<在CT#中你可以使用:
- (typeName)
<柯****** @ sympatico.ca>在消息中写道
新闻:ee ************** @ tk2msftngp13.phx.gbl ...
In VB.NET you can use:
- CType
- DirectCast

In C# you can use:
- (typeName)
"Ken Allen" <ke******@sympatico.ca> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
我对.Net比较新,但是多年来一直在使用VB和C / C ++。

VB6及其早期的一个缺点是难以将记录转换为不同的 '形状''所以人们可以对它进行不同的操作。例如,我有一个复杂的数据结构,我可以在VB6 TYPE声明中表示,但是我不能轻易地将它转换为固定长度的无符号字节数组
以便我可以对内容执行校验和计算。
另一种情况是从文件中读取缓冲区,然后根据某些标题信息解释内容。这在VB6中也非常困难。

VB.Met和C#中有哪些设施(没有指针,这使得这很容易,尽管容易出错) ,在C和C ++中)?

-Ken
I am relatively new to .Net, but have been using VB and C/C++ for
years.

One of the drawbacks with VB6 and earlier was the difficulty in
casting a ''record'' to a different ''shape'' so one could perform
different manipulations on it. For example, I have a complex data
structure, which I can represent in a VB6 TYPE declaration, but I
cannot easily convert that to a fixed length array of unsigned bytes
so that I could perform a checksum calculation on the contents.
Another case is where a read a buffer from a file and then wish to
interpret the contents based on some header information. This was
also very difficult in VB6.

What facilities are available in VB.Met and C# (which does not have
pointers, which made this so easy, albeit error prone, in C and C++)?

-Ken




问候 - OHM#在********** @ BTInternet.com


" ;一名男子[OHM#]"

< OneHandedMan @& REMOVE& TO%MAIL%MEBTInternet.com> schrieb
"One Handed Man [ OHM# ]"
<OneHandedMan@&REMOVE&TO%MAIL%MEBTInternet.com> schrieb
只是为了澄清CType和DirectCast之间的区别。

DirectCast只能用于参考类型
并且它们必须是同一类型。然而,CType
可用于值类型和参考类型
,其中铸件是允许的。


只有一条评论:当对象

被装箱时,DirectCast也可以与值类型一起使用:


Dim o As Object

Dim i As Integer

o = 17I

i = DirectCast(o,Integer)

@ Ken:

DirectCast执行类型转换,而CType可以执行类型转换

_和_类型转换。类型转换仅更改引用的类型,

但它不会创建新对象。类型转换会创建一个新的

对象。

DirectCast在运行时比CType更快。
Just to clarify the difference between CType and DirectCast.

DirectCast can only be used on reference types
and they have to be the same type. Whereas, CType
can be used on both Value Types and Reference Types
where the casting is permissable.
Only a remark: DirectCast can also be used with value types when the object
is boxed:

Dim o As Object
Dim i As Integer
o = 17I
i = DirectCast(o, Integer)
@Ken:
DirectCast performs type casting whereas CType can perform type casting
_and_ type conversion. Type casting only changes the type of the reference,
but it does not create a new object. Type conversion does create a new
object.
DirectCast is faster at runtime than CType.




情况?我之前也读过这篇文章,但我无法重现

。速度相等。

-

Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html



Under which circumstances? I read this also before but I could not reproduce
it. The speed was equal.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


这篇关于在VB.Net和C#中进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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