C#中的动态数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/594853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:38:40  来源:igfitidea点击:

Dynamic array in C#

c#

提问by Siddiqui

Is there any method for creating a dynamic array in C#?

有没有什么方法可以在 C# 中创建动态数组?

采纳答案by Chris Van Opstal

Take a look at Generic Lists.

看看通用列表

回答by Migol

List<T>for strongly typed one, or ArrayListif you have .NET 1.1 or love to cast variables.

List<T>对于强类型,或者ArrayList如果您有 .NET 1.1 或喜欢强制转换变量。

回答by JaredPar

Expanding on Chris and Migol`s answer with a code sample.

使用代码示例扩展 Chris 和 Migol 的回答。

Using an array

使用数组

Student[] array = new Student[2];
array[0] = new Student("bob");
array[1] = new Student("joe");

Using a generic list. Under the hood the List<T> class uses an array for storage but does so in a fashion that allows it to grow effeciently.

使用通用列表。在幕后, List<T> 类使用数组进行存储,但这样做的方式可以使其有效增长。

List<Student> list = new List<Student>();
list.Add(new Student("bob"));
list.Add(new Student("joe"));
Student joe = list[1];

回答by Mehdi LAMRANI

Sometimes plain arrays are preferred to Generic Lists, since they are more convenient (Better performance for costly computation -Numerical Algebra Applications for example, or for exchanging Data with Statistics software like R or Matlab)

有时,普通数组比通用列表更受欢迎,因为它们更方便(对于昂贵的计算,例如数值代数应用程序,或与 R 或 Matlab 等统计软件交换数据的性能更好)

In this case you may use the ToArray() method after initiating your List dynamically

在这种情况下,您可以在动态启动 List 后使用 ToArray() 方法

List<string> list = new List<string>();
list.Add("one");
list.Add("two");
list.Add("three");

string[] array = list.ToArray();

Of course, this has sense only if the size of the array is never known nor fixed ex-ante. if you already know the size of your array at some point of the programit is better to initiate it as a fixed length array. (If you retrieve data from a ResultSet for example, you could count its size and initiate an array of that size, dynamically)

当然,这只有在数组的大小从不知道也不预先固定时才有意义。如果您在程序的某个点已经知道数组的大小,最好将其作为固定长度的数组启动。(例如,如果您从 ResultSet 检索数据,您可以计算其大小并动态启动该大小的数组)

回答by Shubham Gupta

Use the array listwhich is actually implement array. It takes initially array of size 4 and when it gets full, a new array is created with its double size and the data of first array get copied into second array, now the new item is inserted into new array. Also the name of second array creates an alias of first so that it can be accessed by the same name as previous and the first array gets disposed

使用实际实现数组的数组列表。它最初采用大小为 4 的数组,当它变满时,将创建一个具有双倍大小的新数组,并将第一个数组的数据复制到第二个数组中,现在将新项目插入到新数组中。此外,第二个数组的名称创建了一个别名 first,以便可以通过与前一个相同的名称访问它,并处理第一个数组

回答by sydur.rahman21

Dynamic Array Example:

动态数组示例:

Console.WriteLine("Define Array Size? ");
int number = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter numbers:\n");
int[] arr = new int[number];

for (int i = 0; i < number; i++)
{
    arr[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < arr.Length; i++ )
{
    Console.WriteLine("Array Index: "+i + " AND Array Item: " + arr[i].ToString());
}
Console.ReadKey();

回答by Avner

You can do this with dynamic objects:

您可以使用动态对象执行此操作:

var dynamicKeyValueArray = new[] { new {Key = "K1", Value = 10}, new {Key = "K2", Value = 5} };

foreach(var keyvalue in dynamicKeyValueArray)
{
    Console.Log(keyvalue.Key);
    Console.Log(keyvalue.Value);
}