Enumerable.Empty Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns an empty IEnumerable
public:
generic
static System::Collections::Generic::IEnumerable ^ Empty();
public static System.Collections.Generic.IEnumerable Empty();
static member Empty : unit -> seq<'Result>
Public Function Empty(Of TResult) () As IEnumerable(Of TResult)
Type Parameters
- TResult
The type to assign to the type parameter of the returned generic IEnumerable
Returns
An empty IEnumerableTResult.
Examples
The following code example demonstrates how to use Empty
IEnumerable empty = Enumerable.Empty();
' Create an empty sequence.
Dim empty As IEnumerable(Of Decimal) = Enumerable.Empty(Of Decimal)()
The following code example demonstrates a possible application of the Empty
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 namesList =
new List { names1, names2, names3 };
// Only include arrays that have four or more elements
IEnumerable allNames =
namesList.Aggregate(Enumerable.Empty(),
(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)
Next
' 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
Remarks
The EmptyTResult. When the object it returns is enumerated, it yields no elements.
In some cases, this method is useful for passing an empty sequence to a user-defined method that takes an IEnumerable