Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Enumerable Class
 Empty(TResult) Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Enumerable..::.Empty<(Of <(TResult>)>) Method

Returns an empty IEnumerable<(Of <(T>)>) that has the specified type argument.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)
Visual Basic (Declaration)
Public Shared Function Empty(Of TResult) As IEnumerable(Of TResult)
Visual Basic (Usage)
Dim returnValue As IEnumerable(Of TResult)

returnValue = Enumerable.Empty()
C#
public static IEnumerable<TResult> Empty<TResult>()
Visual C++
public:
generic<typename TResult>
static IEnumerable<TResult>^ Empty()
JScript
JScript does not support generic types or methods.

Type Parameters

TResult

The type to assign to the type parameter of the returned generic IEnumerable<(Of <(T>)>).

Return Value

Type: System.Collections.Generic..::.IEnumerable<(Of <(TResult>)>)
An empty IEnumerable<(Of <(T>)>) whose type argument is TResult.

The Empty<(Of <(TResult>)>)()()() method caches an empty sequence of type TResult. 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<(Of <(T>)>). It can also be used to generate a neutral element for methods such as Union. See the Example section for an example of this use of Empty<(Of <(TResult>)>)()()().

The following code example demonstrates how to use Empty<(Of <(TResult>)>)()()() to generate an empty IEnumerable<(Of <(T>)>).

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

The following code example demonstrates a possible application of the Empty<(Of <(TResult>)>)()()() method. The Aggregate method is applied to a collection of string arrays. The elements of each array in the collection are added to the resulting IEnumerable<(Of <(T>)>) only if that array contains four or more elements. Empty<(Of <(TResult>)>) is used to generate the seed value for Aggregate because if no array in the collection has four or more elements, only the empty sequence is returned.

Visual Basic
' 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.
MsgBox(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

C#
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
*/

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

.NET Compact Framework

Supported in: 3.5

XNA Framework

Supported in: 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker