c# - 如何在 C# 中创建一维动态数组?

标签 c# arrays dynamic

C# 菜鸟问题:如何创建一维动态数组?以及以后如何更改?

谢谢。

最佳答案

您可以使用 List<> 而不是使用数组C# 中的对象。

List<int> integerList = new List<int>();

要迭代列表中包含的项目,请使用 foreach运算符(operator):

foreach(int i in integerList)
{
    // do stuff with i
}

您可以使用 Add() 在列表对象中添加项目和 Remove()功能。

for(int i = 0; i < 10; i++)
{
    integerList.Add(i);
}

integerList.Remove(6);
integerList.Remove(7);

您可以转换 List<T>使用 ToArray() 到数组功能:

int[] integerArray = integerList.ToArray();

这是 documentationList<>对象。

关于c# - 如何在 C# 中创建一维动态数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2676291/

相关文章:

c# - 我将如何在 mvc 4 中将以下语句的值设置为零

c++ - 如何设置一个字符串数组,其中包含将通过数组显示在 iphone 文本上的字符串?

c - strtok() 的问题

javascript - 如何合并数组中的重复条目

validation - Grails:如何使g:textfield只接受数字或字母?

jsf - 动态 UI 包含在 Primefaces 对话框中

c# - 将对象/动态转换为匿名类型

c# - 为什么在使用样本生成器时会得到循环引用 DummyApiController --> DummyApiController?

c# - 如何从 DataTemplate 派生一个新类,然后用它代替 DataTemplate?

c# - 从java到c#的base64编码和解码问题