Passing Datatables by Reference

  • Thread starter Thread starter William Ryan
  • Start date Start date
W

William Ryan

I thought I understood this subject, but something happened today that
proved me wrong...

My boss has a class that takes a DataTable passed by Reference and Add some
columns to it (in this example 18). So an app he was working on was taking
like 3 seconds to fill the DataTable each time it was called.

I suggested using a DataReader b/c of the way the query was being used.
However, in order not to break the class, he wanted it to return a DataTable
(which is still passed in by Ref). Anyway, adding the 18 columns that were
needed took 3 seconds.

I was really surprised that adding 18 columns was taking longer than
iterating through 100+ rows or 18 columns and adding them to the datatable.
The latter happened instantly, but the adding of the columns took 3-5
seconds.

I suspected the reference type (datatable) might be causing some performance
hit, but not that much of one. Anyway, I declared a table locally and tried
adding 18 columns to it. Same function, locally declared datatable, filled
instantly. So to not break the code, he set the data table that was passed
in equal to the one we declared locally, and still, no performance problem.

So my question is this....are reference types that bad performance wise when
you pass them., and if so, I guess some are much worse than others (namely
the Datatable)?

TIA,

Bill
 
Bill,
Correct me if I'm wrong, but objects are automatically
passed like a pointer would be. You don't have to
explicitely specify ref in your parameter list when
passing any datatype that is a reference type.

Notice in the example below I don't specify the "ref"
keyword when passing the DataTable to myclassB. It is my
understanding you don't have to for reference types.

public class myclassA
{
private DataTable _myTable = null;

public myclassA()
{
myclassB o = new myclassB();
o.ChangeTable(table);
}

}

public class myclassB
{
public myclassB(){}

public void ChangeTable(DataTable table)
{
//add code here to modify table
//whatever was changed here should
//also be changed in myclassA
}
}

HTH
 
Yes, there is a distinction between passing Value and Reference types,
although you can pass reference types by value (in which case you pass a
copy of the reference).

Even if the table was passed by value, the problem would still persist b/c
we're dealing with a reference type.

I'm just surprised that passing in a Reference type and adding columns was
that much slower. I knew there was a performance hit due to resource
management of the object, but I didn't realize it was that substantial.

Thanks for the reply though, much appreciated.

Bill
 
Back
Top