VB.NET Dim vs.新 [英] VB.NET Dim vs. New

查看:81
本文介绍了VB.NET Dim vs.新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下构造之间有什么区别?为什么偏爱一个?

What are the differences between the following constructs? Why prefer one over the other?

第一名:

Dim byteArray(20) as Byte

第二名:

Dim byteArray() as Byte = new Byte(20) {}

任何帮助将不胜感激.

Any help would be appreciated.

谢谢.

编辑-我更正了一些代码. "ReDim"应为"Dim".

Edit - I corrected some code. "ReDim" should be "Dim".

推荐答案

它们都在托管堆上分配20个字节.

They both allocate 20 bytes on the managed heap.

它们都将标识符"byteArray"设置为指向这些字节.

They both set the identifier 'byteArray' to point to those bytes.

带有"new"运算符(子句)的语句允许初始化数组元素.

The statement with the "new" operator (clause) allows initialization of the array elements.


Dim byteArray() as Byte = new Byte(20) { 1, 2, 3, 4, 5, 6, ... }

顺便说一句,要为一个维度分配一个不包含大小为-1的元素的数组.如果您需要访问诸如length之类的属性而不会引发错误,这将很有用.

Incidentally, to allocate an array with no elements specifiy a size of -1 for one of the dimensions. This is useful if you need to access properties like length without throwing an error.

这篇关于VB.NET Dim vs.新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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