布尔是.NET两个字节? [英] Boolean is two bytes in .NET?

查看:171
本文介绍了布尔是.NET两个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题从源于这里。我想这个说法在VS,看看会发生什么:

 莱恩(名称< = 3)
 

根据这个答案也的这个,布尔要消耗4个字节。根据MSDN, LEN函数在VB

  

返回包含字符的任一数目的整数   串或存储变量所需要的字节数标称

名称和LT = 3 应转换3到字符串并执行字符串比较,返回一个布尔值,所以莱恩应该评估它字节,这应该是4,由于某些原因,上述code返回2,不管第二个参数的数量。换句话说,莱恩(真)也返回2.尝试不同的平台的目标(32和64) - 同样的事情

为什么莱恩(布尔)返回2,而不是4?

解决方案
  

使用用户定义的类型和对象的变量,Len函数返回,因为它将由FilePut功能被写入到文件的大小。如果一个对象包含一个字符串,它会返回字符串的长度。如果一个对象包含任何其他类型的,它会返回对象的大小,因为这将被写入到由FilePut函数的文件。

莱恩()是一个遗留的功能,它仅应在code使用的是从previous一代的Visual Basic项目移植。凡通常会出现在code,涉及VB值的二进制序列化。基本类型的大小是在那些旧版本不同。 整数是16位为例,解释不好PInvoke的声明,您在使用龙网找到大量。而布尔是一个VARIANT_BOOL的引擎盖下,一个16位值。通过将其值转换为-1而不是1。此外古怪。

显然,采用.NET的大小将是一个沉重的重大更改,以大量的数据存在于写的VB6程序文件。或在一个TCP连接发送的二进制数据。等等。因此,莱恩()函数返回遗留的大小。

This question originates from here. I tried this statement in VS to see what happens:

Len(Name <= 3)

According to this answer and also this one, Boolean should consume 4 bytes. According to MSDN, Len Function in VB

Returns an integer containing either the number of characters in a string or the nominal number of bytes required to store a variable.

Name <= 3 should convert 3 to String and perform string comparison, returning a boolean value, so Len should evaluate number of bytes in it, which should be 4. For some reason, above code returns 2, regardless of the second parameter. In other words, Len(True) also returns 2. Tried for different platform targets (32 and 64) - same thing.

Why does Len(Boolean) return 2 instead of 4?

解决方案

With user-defined types and Object variables, the Len function returns the size as it will be written to the file by the FilePut function. If an Object contains a String, it will return the length of the string. If an Object contains any other type, it will return the size of the object as it will be written to the file by the FilePut function.

Len() is a legacy function, it should only be used in code that was ported from previous generation Visual Basic projects. Where it would typically appear in code that involved binary serialization of VB values. Sizes of basic types were different in those old versions. Integer was 16 bits for example, explaining the great number of bad pinvoke declarations you find in the web that use Long. And Boolean was a VARIANT_BOOL under the hood, a 16-bit value. Furthermore quirky by having its True value convert to -1 instead 1.

Clearly adopting the .NET sizes would have been a heavily breaking change to a lot of data that exists in files written by a VB6 program. Or binary data sent across a TCP connection. Etcetera. Accordingly, the Len() function returns the legacy sizes.

这篇关于布尔是.NET两个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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