简体   繁体   English

C# 数组类型列表

[英]C# list of array type

I want to create a list of array type.我想创建一个数组类型的列表。 I want to create array containing values :我想创建包含值的数组:

array = [a,b];

Then i want to put this array in list :然后我想把这个数组放在列表中:

List<Array> list = new List<Array>( );

I am able to do this with list of string type but no luck with array type :我可以用字符串类型列表来做到这一点,但对数组类型没有运气:

List<String> list = new List<String>( );

I am from javascript background, not much familiar with concept of collections in c#.我来自 javascript 背景,对 c# 中的集合概念不太熟悉。 Also how can i create array in c# like we do in javascript :另外,我如何像在 javascript 中那样在 c# 中创建数组:

var arrTemp = ["a", "b"];

Well, since your array is string[] :好吧,因为你的数组是string[]

 var arrTemp = ["a", "b"]; 

you have to declare the required list as List<string[]> :您必须将所需list声明为List<string[]>

 // list of string arrays
 List<string[]> list = new List<string[]>() {
   new string[] {"a", "b"} 
 };

In case you want to be able to put any array into the list declare it as loose as possible ( List<object[]> ):如果您希望能够将任何数组放入list ,请尽可能将声明为松散( List<object[]> ):

 // list of abitrary arrays
 List<object[]> list = new List<object[]>() {
   new string[] {"a", "b"},
   new double[] {123.45, 789.12, 333.55}, 
   new object[] {'a', "bcd", 1, 244.95, true},
 };

希望这可以帮到你

var test = new List<int[]>();

You can actually create a list of arrays:您实际上可以创建一个数组列表:

var listOfArrays = new List<Array>();

The problem with this is that it's difficult to use the arrays themselves, as the Array type doesn't support array syntax.这样做的问题是很难使用数组本身,因为Array类型不支持数组语法。 (eg You can't do listOfArrays[0][0] ) Instead, you have to use the GetValue method to do your retrieval: (例如,您不能执行listOfArrays[0][0] )相反,您必须使用GetValue方法来进行检索:

var obj = listOfArrays[0].GetValue(0);

But this has another problem.但这还有另一个问题。 The GetValue method returns object , so while you could always cast it to the desired type, you lose your type safety in choosing this approach. GetValue方法返回object ,因此虽然您始终可以将其强制转换为所需的类型,但选择此方法会失去类型安全性。

Alternatively, you could just store object[] arrays:或者,您可以只存储object[]数组:

var listOfArrays = new List<object[]>();
var obj = listOfArrays[0][0];

But while this solves the issue of the array notation, you still lose the type safety.但是虽然这解决了数组符号的问题,但你仍然失去了类型安全性。

Instead, if at all possible, I would recommend finding a particular type, then just have arrays of that type:相反,如果可能的话,我会建议找到特定类型,然后只使用该类型的数组:

var listOfArrays = new List<string[]>();
string s = listOfArrays[0][0];

for example, an array of strings would be例如,一个字符串数组将是

var arrayOfString = new string[]{"a","b"};
// or shorter form: string[] arrayOfString  = {"a","b"};
// also: var arrayOfString = new[] { "a", "b" }

And then creating a list-of-arrayOfString would be然后创建一个 list-of-arrayOfString 将是

var listOfArrayOfString = new List<string[]>();

This works with any type, for example if you had a class MyClass这适用于任何类型,例如,如果您有一个类MyClass

var arrayOfMyClass = new MyClass[]{ ... }; // ... is you creating instances of MyClass
var list = new List<MyClass[]>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM