在 C# 中将 IEnumerable 转换为列表
本文将说明将数据从 IEnumerable 转换为 C# 中的列表。
在 C# 中使用 ToList()
将数据从 IEnumerable 转换为列表
IEnumerable 是一个包含在 System.Collections.Generic
命名空间中的接口。像所有其他接口一样,它公开了一个方法。
此案例公开了 enumerator
方法,该方法支持迭代或循环遍历泛型和非泛型列表,包括 LINQ 查询和数组。
IEnumerable 仅包含返回 IEnumerator 对象的 GetEnumerator
方法。
public interface IEnumerable<out T> : System.Collections.IEnumerable
IEnumerable 接口返回的值是只读的。这意味着操作仅限于这些数据。
C# 中的 ToList()
方法是操作这些数据的替代方法。C# 列表中的元素可以添加、删除、排序和重新排列。
与 IEnumerable 值相比,可以对列表执行的操作太多了。
C# List
类表示可以通过索引访问的强类型对象的集合。ToList()
函数或方法位于 System.Linq
命名空间(语言集成查询)中。
LINQ 中的 ToList
运算符从给定源获取元素并返回一个新列表。输入将被转换为类型列表。
ToList()
方法返回字符串实例的列表。ToList()
函数可以在数组引用或 IEnumerable 值上调用,反之亦然。
可以按如下方式访问或操作列表元素:
// 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);
}
}
}
}
输出:
1
2
3
4
5
6
7
8
9
10
在 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);
}
}
}
}
输出:
1
2
3
4
5
6
7
8
9
10
在 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
1
2
3
4
5
Output as Enumerable
1
2
3
4
5
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径