In a DataTable
object, is there added overhead to looking up a column value by name thisRow("ColumnA")
rather than by the column index thisRow(0)
? In which scenarios might this be an issue.
I work on a team that has lots of experience writing VB6 code and I noticed that didn't do column lookups by name for DataTable
objects or data grids. Even in .NET code, we use a set of integer constants to reference column names in these types of objects. I asked our team lead why this was so, and he mentioned that in VB6, there was a lot of overhead in looking up data by column name rather than by index. Is this still true for .NET?
Example code (in VB.NET, but same applies to C#):
Public Sub TestADOData()
Dim dt As New DataTable
'Set up the columns in the DataTable '
dt.Columns.Add(New DataColumn("ID", GetType(Integer)))
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Description", GetType(String)))
'Add some data to the data table '
dt.Rows.Add(1, "Fred", "Pitcher")
dt.Rows.Add(3, "Hank", "Center Field")
'Method 1: By Column Name '
For Each r As DataRow In dt.Rows
Console.WriteLine( _
"{0,-2} {1,-10} {2,-30}", r("ID"), r("Name"), r("Description"))
Next
Console.WriteLine()
'Method 2: By Column Name '
For Each r As DataRow In dt.Rows
Console.WriteLine("{0,-2} {1,-10} {2,-30}", r(0), r(1), r(2))
Next
End Sub
Is there an case where method 2 provides a performance advantage over method 1?