添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
public:
generic <typename TResult>
 static System::Collections::Generic::IEnumerable<TResult> ^ Empty();
public static System.Collections.Generic.IEnumerable<TResult> Empty<TResult> ();
static member Empty : unit -> seq<'Result>
Public Function Empty(Of TResult) () As IEnumerable(Of TResult)
TResult

下面的代码示例演示如何使用 Empty<TResult>() 生成空 IEnumerable<T>

IEnumerable<decimal> empty = Enumerable.Empty<decimal>(); ' Create an empty sequence. Dim empty As IEnumerable(Of Decimal) = Enumerable.Empty(Of Decimal)()

下面的代码示例演示 了 方法的 Empty<TResult>() 可能应用。 方法 Aggregate 应用于字符串数组的集合。 仅当该数组包含四个或更多个元素时,集合中每个数组的元素才会添加到生成的 IEnumerable<T> Empty 用于生成 的 Aggregate 种子值,因为如果集合中没有数组包含四个或更多个元素,则只返回空序列。

string[] names1 = { "Hartono, Tommy" }; string[] names2 = { "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu" }; string[] names3 = { "Solanki, Ajay", "Hoeing, Helge", "Andersen, Henriette Thaulow", "Potra, Cristina", "Iallo, Lucio" }; List<string[]> namesList = new List<string[]> { names1, names2, names3 }; // Only include arrays that have four or more elements IEnumerable<string> allNames = namesList.Aggregate(Enumerable.Empty<string>(), (current, next) => next.Length > 3 ? current.Union(next) : current); foreach (string name in allNames) Console.WriteLine(name); This code produces the following output: Adams, Terry Andersen, Henriette Thaulow Hedlund, Magnus Ito, Shu Solanki, Ajay Hoeing, Helge Potra, Cristina Iallo, Lucio ' Create three string arrays. Dim names1() As String = {"Hartono, Tommy"} Dim names2() As String = {"Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu"} Dim names3() As String = {"Solanki, Ajay", "Hoeing, Helge", "Andersen, Henriette Thaulow", "Potra, Cristina", "Iallo, Lucio"} ' Create a List that contains 3 elements, where ' each element is an array of strings. Dim namesList As New List(Of String())(New String()() {names1, names2, names3}) ' Select arrays that have four or more elements and union ' them into one collection, using Empty() to generate the ' empty collection for the seed value. Dim allNames As IEnumerable(Of String) = namesList.Aggregate(Enumerable.Empty(Of String)(), Function(current, nextOne) _ IIf(nextOne.Length > 3, current.Union(nextOne), current)) Dim output As New System.Text.StringBuilder For Each name As String In allNames output.AppendLine(name) ' Display the output. Console.WriteLine(output.ToString()) ' This code produces the following output: ' Adams, Terry ' Andersen, Henriette Thaulow ' Hedlund, Magnus ' Ito, Shu ' Solanki, Ajay ' Hoeing, Helge ' Potra, Cristina ' Iallo, Lucio

方法 Empty<TResult>() 缓存 类型 TResult 为 的空序列。 当枚举它返回的对象时,它不生成任何元素。

在某些情况下,此方法可用于将空序列传递给采用 IEnumerable<T> 的用户定义方法。 它还可用于为 等 Union 方法生成中性元素。 有关此用法 Empty<TResult>() 的示例,请参阅示例部分。

Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback .

提交和查看相关反馈