Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is DataTable passed by reference? [duplicate]

Tags:

c#

.net

This might sound like a stupid question, but I just happened to stumbled upon this. I know you can use ref to pass a parameter by reference. But I have this method

    public void SaveRecordsIntoTemporaryTable(DataTable objDataTable, string userSessionID)
   {

       // The objDataTable has 5 columns "Id", "Name", "Tag_1", "Tag_2", "Tag_3"
       // Now in here I remove "Tag_1", "Tag_2", and "Tag_3"
       objDataTable.Columns.Remove("Tag_1");
       objDataTable.Columns.Remove("Tag_2");
       objDataTable.Columns.Remove("Tag_3");

       ...

   }

Now I have set a debug point on third line, but after I remove this column "Tag_3", and I hover my cursor over objDataTable in the parameter, the DataTable it shows also has the columns removed? So, is it getting passed by reference?

UPDATE

Okay, if it is getting passed by reference, what difference would it make if I use ref?

like image 818
Razort4x Avatar asked Jan 30 '26 15:01

Razort4x


1 Answers

So, is it getting passed by reference?

Its address is passed by value. Since DataTable is a class (a reference type), Its address gets passed by value and hence you see the change.

Try doing:

objDataTable = null;

If it is truly passed by reference then you will see the caller setting the DataTable to null but that doesn't happen.

See: Parameter passing in C# by Jon Skeet

like image 176
Habib Avatar answered Feb 02 '26 07:02

Habib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!