Please take a look at the code below:
Public Class PersonTest
Public Function ListTest()
Dim list As List(Of Person) = New List(Of Person)
Dim p1 As Person = New Person(1, "Ian")
Dim p2 As Person = New Person(2, "Steven")
Dim p3 As Person = New Person(3, "Sharon")
list.Add(p1)
list.Add(p2)
list.Add(p3)
For Each p As Person In list
MsgBox(p.IDNumber)
Next
End Function
End Class
Public Class Person
Public IDNumber As Integer
Public Name As String
Public Sub New(ByVal id As Integer, ByVal name As String)
IDNumber = id
name = name
End Sub
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim pt As PersonTest = New PersonTest
pt.ListTest()
End Sub
End Class
I expect the Message Box to print: 1,2,3 which it does. Can you confirm that List uses insertion order. I am planning to store the results of database queries in a list i.e:
SELECT * FROM Person ORDER BY ID
The SQL statement is ordered by ID. Can I assume that the List will also be ordered by ID?
The reason I ask is because the answerer to my question here: Data Access Layer returns DataTable, suggests using a List to return an object from a Data Access Layer.