當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Enumerable.DefaultIfEmpty方法代碼示例

本文整理匯總了VB.NET中System.Linq.Enumerable.DefaultIfEmpty方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET Enumerable.DefaultIfEmpty方法的具體用法?VB.NET Enumerable.DefaultIfEmpty怎麽用?VB.NET Enumerable.DefaultIfEmpty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。


在下文中一共展示了Enumerable.DefaultIfEmpty方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: DefaultIfEmptyEx2

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub DefaultIfEmptyEx2()
    ' Create a Pet object to use as the default value.
    Dim defaultPet As New Pet With {.Name = "Default Pet", .Age = 0}

    ' Create a List of Pet objects.
    Dim pets1 As New List(Of Pet)(New Pet() _
                          {New Pet With {.Name = "Barley", .Age = 8},
                           New Pet With {.Name = "Boots", .Age = 4},
                           New Pet With {.Name = "Whiskers", .Age = 1}})

    Dim output1 As New System.Text.StringBuilder
    ' Enumerate the items in the list, calling DefaultIfEmpty() 
    ' with a default value.
    For Each pet As Pet In pets1.DefaultIfEmpty(defaultPet)
        output1.AppendLine("Name: " & pet.Name)
    Next

    ' Display the output.
    Console.WriteLine(output1.ToString())

    ' Create an empty List.
    Dim pets2 As New List(Of Pet)

    Dim output2 As New System.Text.StringBuilder
    ' Enumerate the items in the list, calling DefaultIfEmpty() 
    ' with a default value.
    For Each pet As Pet In pets2.DefaultIfEmpty(defaultPet)
        output2.AppendLine("Name: " & pet.Name)
    Next

    ' Display the output.
    Console.WriteLine(output2.ToString())
End Sub
開發者ID:VB.NET開發者,項目名稱:System.Linq,代碼行數:38,代碼來源:Enumerable.DefaultIfEmpty

輸出:

Name: Barley
Name: Boots
Name: Whiskers

Name: Default Pet

示例2: DefaultIfEmptyEx1

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub DefaultIfEmptyEx1()
    ' Create a List of Pet objects.
    Dim pets As New List(Of Pet)(New Pet() _
                         {New Pet With {.Name = "Barley", .Age = 8},
                          New Pet With {.Name = "Boots", .Age = 4},
                          New Pet With {.Name = "Whiskers", .Age = 1}})

    Dim output As New System.Text.StringBuilder
    ' Iterate through the items in the List, calling DefaultIfEmpty().
    For Each pet As Pet In pets.DefaultIfEmpty()
        output.AppendLine(pet.Name)
    Next

    ' Display the output.
    Console.WriteLine(output.ToString())
End Sub
開發者ID:VB.NET開發者,項目名稱:System.Linq,代碼行數:21,代碼來源:Enumerable.DefaultIfEmpty

輸出:

Barley
Boots
Whiskers

示例3: List

' Create an empty List.
Dim numbers As New List(Of Integer)()

Dim output As New System.Text.StringBuilder
' Iterate through the items in the List, calling DefaultIfEmpty().
For Each number As Integer In numbers.DefaultIfEmpty()
    output.AppendLine(number)
Next

' Display the output.
Console.WriteLine(output.ToString())
開發者ID:VB.NET開發者,項目名稱:System.Linq,代碼行數:11,代碼來源:Enumerable.DefaultIfEmpty

輸出:

0


注:本文中的System.Linq.Enumerable.DefaultIfEmpty方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。