C#中长度未知的数组 [英] Array of an unknown length in C#

查看:52
本文介绍了C#中长度未知的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 C#,在数组介绍中,他们展示了如何将变量建立为数组,但似乎必须在赋值时指定数组的长度,如果我不知道怎么办数组的长度?

I've just started learning C# and in the introduction to arrays they showed how to establish a variable as an array but is seems that one must specify the length of the array at assignment, so what if I don't know the length of the array?

推荐答案

必须为数组分配一个长度.要允许任意数量的元素,请使用 List 类.

Arrays must be assigned a length. To allow for any number of elements, use the List class.

例如:

List<int> myInts = new List<int>();
myInts.Add(5);
myInts.Add(10);
myInts.Add(11);
myInts.Count // = 3

这篇关于C#中长度未知的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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