可通过此
参考
进行进一步讨论。
可以按如下方式访问或操作列表元素:
// an array of 4 strings
string[] animals = { "Cow", "Camel", "Elephant" };
//creating a new instance of a list
List<string> animalsList = new List<string>();
//use AddRange to add elements
animalsList.AddRange(animals);
// declaring a list and passing array elements into the list
List<string> animalsList = new List<string>(animals);
//Adding an element to the collection
animalsList.Add("Goat");
Console.WriteLine(animalsList[2]); // Output Elephant, Accessing elements of a list
// Collection of new animals
string[] newAnimals = { "Sheep", "Bull", "Camel" };
// Insert array at position 3
animalsList.InsertRange(3, newAnimals);
// delete 2 elements at starting with element at position 3
animalsList.RemoveRange(3, 2);
必须导入命名空间才能使用
ToList
函数,如下所示:
using System.Collections.Generic;
using System.Linq;
下面是
ToList()
方法的语法。
List<string> result = countries.ToList();
下面是在 C# 中将 IEnumerable 转换为列表的示例代码。
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestDomain
class Program
public static void Main(String[] args)
IEnumerable<int> testValues = from value in Enumerable.Range(1, 10) select value;
List<int> result = testValues.ToList();
foreach (int a in result)
Console.WriteLine(a);
在 C# 中使用
ToList()
将数据从数组转换为列表
下面是在 C# 中将数组转换为列表的示例代码。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayToListApp
class Program
static void Main(string[] args)
//create an array of African countries of type string containing the collection of data
string[] countries = { "Nigeria", "Ghana", "Egypt", "Liberia", "The Gambia", "Morocco", "Senegal" };
//countries.ToList() convert the data collection into the list.
List<string> result = countries.ToList();
//foreach loop is used to print the countries
foreach (string s in result)
Console.WriteLine(s);
Nigeria
Ghana
Egypt
Liberia
The Gambia
Morocco
Senegal
在 C# 中使用 ToArray()
将数据从列表转换为数组
下面是在 C# 中从列表转换为数组的示例代码。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListToArrayApp
class Program
static void Main(string[] args)
//create an array of African countries of type string containing the collection of data
IEnumerable<int> testValues = from value in Enumerable.Range(1, 10) select value;
//countries.ToList() convert the data collection into the list.
List<int> result = testValues.ToList();
int[] array = result.ToArray();//convert string to array.
//foreach loop is used to print the countries
foreach (int i in array)
Console.WriteLine(i);
在 C# 中使用 AsEnumerable()
将数据从 List 转换为 IEnumerable
下面是将 IEnumerable 转换为列表并返回 IEnumerable 的示例代码。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListToArrayApp
class Program
static void Main(string[] args)
List<int> list = new List<int>();
IEnumerable enumerable = Enumerable.Range(1, 5);
foreach (int item in enumerable) {
list.Add(item);
Console.WriteLine("Output as List");
foreach (var item in list) {
Console.WriteLine(item);
Console.WriteLine("Output as Enumerable");
//using the AsEnumerable to convert list back to Enumerable
IEnumerable resultAsEnumerable = list.AsEnumerable();
foreach (var item in resultAsEnumerable) {
Console.WriteLine(item);
Output as List
Output as Enumerable
Abdullahi is a full-stack developer and technical writer with over 5 years of experience designing and implementing enterprise applications. He loves taking on new challenges and believes conceptual programming theories should be implemented in reality.
LinkedIn
GitHub