[英]How do I initialize a list of string arrays in C#?
I've looked around but can't find my answer. 我环顾四周,但找不到答案。
I have this code: 我有以下代码:
List<String[]> _myList = new List<String[]>();
I just want to initialize it with values but I don't know how. 我只想用值初始化它,但不知道如何。 Thanks a lot 非常感谢
Try this: 尝试这个:
List<String[]> _myList = new List<String[]>{new String[]{"a","b"},new String[]{"c","d"}};
This is the List Initializer
syntax. 这是List Initializer
语法。
You could try something like this: 您可以尝试这样的事情:
List<String[]> _myList = new List<String[]> { new String[] { "a", "b", "c", "d"},
new String[] { "a", "b"},
new String[] { "b", "c"} };
This is the collection initializer syntax. 这是集合初始化程序的语法。 For more information about this, please have a look here . 有关此的更多信息,请在此处查看 。
You can do it like this: 您可以这样做:
List<String[]> _myList = new List<String[]>()
{
new string[] { "string", "more string" },
new string[] { "and more string"},
};
or add after initialization like this: 或像这样初始化后添加:
_myList.Add(new string[] {"add more string"});
List<string[]> _myList = new List<string[]>() { new string[1] { "Cool" }, new string[2] { "Also", "cool" } };
First off, a List is not an array in that a list can have a variable number of items in it; 首先,列表不是数组,因为列表中可以包含可变数量的项目; on the other hand, an array has a fixed number of items or members which must be declared and adhered to. 另一方面,数组具有固定数量的项或成员,必须声明并遵守这些项或成员。
using System.Collections.Generic;
Class Program
{
Static Void Main(string[] args)
Var names = new List<string>
{
"Dave",
"Steve",
"Joe",
};
// you can get additional names added by using the Add keyword
names.Add("Hillary")
// If you want to print your list you can use the foreach loop
foreach(string name in names)
Console.Writeline(names);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.