none
(Sumber: milist DOTNET) List(Of x) and IEnumerable(Of x) sebagai return value dari function RRS feed

  • Pertanyaan

  • hi,

    pengen denger pendapat dari teman2,

    mis. ada code seperti berikut:

    bentuk 1

    Public Function GetListAllDataBarang() As IEnumerable(Of Barang)

            Dim listData As List(Of Barang) = Nothing

            Try

                Using cmd As New SqlCommand()

                    With cmd

                        .Connection = conn

                        .CommandText = "Select * From Barang Order By NamaBarang"

                        Using reader As SqlDataReader = .ExecuteReader()

                            If reader.HasRows Then

                                listData = New List(Of Barang)()

                                While reader.Read()

                                    listData.Add(

                                        New Barang With

                                        {

                                            ...

                                        })

                                End While

                            End If

                        End Using

                    End With

                End Using

            Catch ex As Exception

                Throw

            End Try

            Return listData

        End Function

    kemudian dengan contoh pemakaian seperti berikut:

    For Each item As Barang In GetListAllDataBarang()

            ....

    Next

    dengan bentuk 2

    Public Function GetListAllDataBarang() As List(Of Barang)

            Dim listData As List(Of Barang) = Nothing

            Try

                Using cmd As New SqlCommand()

                    With cmd

                        .Connection = conn

                        .CommandText = "Select * From Barang Order By NamaBarang"

                        Using reader As SqlDataReader = .ExecuteReader()

                            If reader.HasRows Then

                                listData = New List(Of Barang)()

                                While reader.Read()

                                    listData.Add(

                                        New Barang With

                                        {

                                            ...

                                        })

                                End While

                            End If

                        End Using

                    End With

                End Using

            Catch ex As Exception

                Throw

            End Try

            Return listData

    End Function

    kemudian dengan contoh pemakaian yang sama:

    For Each item As Barang In GetListAllDataBarang()

            ....

    Next

    proses eksekusi nya sama kagak ya ?? mengingatnya sampai VB 10 sekarang kan blom ada Iterator Yield ( baru ada di VB 11 nanti )

    thanks

    Rabu, 23 Mei 2012 08.33
    Moderator

Jawaban

  • kalau di C# sama aja.

    kalau di C# foreach hanya perduli terhadap IEnumerable object.

    IList itu kan turunan dari IEnumerable jadi sama aja secara performance.

    (yield keyword itu adalah syntactic sugar. coba bayangkan kalau tanpa yield keyword. setiap kali kita mau membuat iterator (kalau di .Net namanya Enumerator) harus mengimplement semua method dari IEnumerator.)

    Dijawab oleh: Agus Syahputra


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Rabu, 23 Mei 2012 08.40
    Moderator