询问初始化1个数组的方式 [英] Ask about the way initializing 1 array

查看:72
本文介绍了询问初始化1个数组的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在使用数组时遇到1麻烦.初始化1数组时,必须声明数组的长度.
有时,数组的长度未知,那么如何初始化数组?
我正在将Visual Studio 2005与.Net Framework 2.0和languague C#一起使用. 请帮助我.

Hi All,

I have 1 trouble about working with array.When initialize 1 array, we must declare the length of array.
Sometimes,the length of the array will not be known, so how to initialize the array ?
I''m using Visual Studio 2005 with .Net Framework 2.0 , languague C#.
Please help me.

推荐答案

声明数组时,必须知道长度,因为数组被视为连续的内存块,必须一起分配.
如果要使用直到填满才知道长度的数组,请考虑使用List< T>.而是使用Add方法仅继续添加元素-以后可以根据需要使用ToArray方法获取数组内容.
When you declare an array, the length has to be known, as the array is considered as a contiguous block of memory, which has to be allocated together.
If you want to use an array where you do not know the length until you have filled it, consider using a List<T> instead and use the Add method to just keep adding elements - you can use the ToArray method later to get the array content if you need it.


这篇关于询问初始化1个数组的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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