Sveučilište u Zagrebu  |  Filozofski fakultet  |  Služba za informatiku  |  Kutak za korisnike  |  Teme

Collection Initializer Extension Function

Imports System.Runtime.CompilerServices

Module Module1

    Class Student
        Public Name As String
        Public Score As Integer
    End Class

    Sub Main()

        Dim x As New Dictionary(Of String, Integer) From {
            {"fred", 1},
            {"jones"},
            {"ann", 15},
            {"steve", 5}
        }

        Dim y As New List(Of Student) From {
            {New Student With {.Name = "fred", .Score = 1}},
            {"jones"},
            {"ann", 15},
            {"steve", 5}
        }

    End Sub

    <Extension()> Sub Add(list As Dictionary(Of String, Integer), name As String)

        list.Add(name, 0)

    End Sub

    <Extension()> Sub Add(list As List(Of Student), name As String, Optional score As Integer = 0)

        list.Add(New Student With {.Name = name, .Score = score})

    End Sub

End Module

 

komentare molim ovdje