Suppose I have the following code snippets, i.e. goo call foo, DataTable created by goo and foo will only call Rows.Add. Then goo will use the updated data of the DataTable instance from foo.
My question is, in my scenario, any differences or benefits compared of using with and using without ref parameter?
I am using C# + .Net 3.5 + VSTS2008.
void foo (ref DataTable dt)
{
// call Row.Add on dt
}
void goo()
{
DataTable dt1 = ...; // create a new instance of DataTable here
foo (ref dt1);
// read updated content of DataTable by foo here
}
You only use
refif you intend to change whatdtpoints to. Eg., if you wanted to replace it with a differentDataTable(or, a type deriving fromDataTable).In this case, you are just using
dt, so there’s no reason to useref. SinceDataTableis a class, any changes made to it will be visible outside offoo.