ASP.NET 2.0 中的数组真的是固定长度的吗?

发布于 2024-09-10 05:17:00 字数 150 浏览 2 评论 0原文

如果我dim一个数组说,5个元素,如果我添加第6个元素,它不会失败吗?我认为这曾经需要redim。在.NET 2.0中,我有一个长度= 3的字符数组。当我从数据库填充它时,一条记录中有4个字符,并且它成功地将所有4个字符添加到数组中?

If I dim an array to say, 5 elements, should it not fail if I go to add a 6th? I thought this used to require a redim. In .NET 2.0, I have a character array of length = 3. When I populate it from the db, one record had 4 characters in it and it successfully added all 4 characters to the array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-09-17 05:17:00

如果将字符数组分配给包含数组(任意大小)的现有数组变量,它将创建所需大小的新数组。原始数组被垃圾收集。

char[] c = new char[3];
c = reader.ReadCharacters(5);  // read 5 characters into new array, assign to c
Debug.Print(c.Length);         // Prints 5. 

If you assign an array of characters to an existing array variable containing an array (of any size), it creates a new array of the size that is required. The original array is garbage collected.

char[] c = new char[3];
c = reader.ReadCharacters(5);  // read 5 characters into new array, assign to c
Debug.Print(c.Length);         // Prints 5. 
调妓 2024-09-17 05:17:00

只是为了添加当前的答案,以防万一这是问题所在。在 VB.NET 中,您可以使用上限而不是所需的长度来声明数组。

例如:

Dim arr(3) as Integer  'length of 4

此数组有 4 个元素,0 - 3。它的长度与您在 C# 中所说的情况相同:

int[] arr = new int[3];  //length of 3

我不知道这是否是您的问题,但以防万一

Just to add to the current answer, in case this was the problem. In VB.NET, you declare arrays with the upper bound, not the length desired.

For example:

Dim arr(3) as Integer  'length of 4

This array has 4 elements, 0 - 3. It does not have a length of 3 as would be the case if you said this in C#:

int[] arr = new int[3];  //length of 3

I don't know if this is your issue, but just in case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文