2

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?

Razort4x
  • 3,296
  • 10
  • 50
  • 88
  • 1
    its because `DataTable` is a reference type. – Daniel A. White Jun 17 '14 at 19:00
  • @Razort4x, With reference to your edit: it is **not** passed by reference. But when you use `ref` keyword, then you can modify the orignal reference like setting it to null – Habib Jun 17 '14 at 19:06

2 Answers2

8

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

CREM
  • 1,929
  • 1
  • 25
  • 35
Habib
  • 219,104
  • 29
  • 407
  • 436
-1

DataTable, and anything else derived from object is passed by reference. If you were to explicitly pass it as ref, then the reference would be by reference - allowing you to change the variable to point to another DataSet (or null).

Edit - ignore the first sentence. It should be DataTable and anything else derived from object is a reference type.

Calling a method passing a reference type by value still allows modifying the properties of the type.

David Crowell
  • 3,711
  • 21
  • 28
  • 5
    No, it is not passed by reference. It *is* a reference, and it is passed by value. This is a very important distinction. – Servy Jun 17 '14 at 19:02
  • 1
    Re-read what I said. *If* he were to pass by reference with `ref`, then it would be a reference to a reference. The existing code does pass a reference by values as you state. – David Crowell Jun 17 '14 at 19:04
  • 2
    It seems you have the correct understanding of what happens. However, it is incorrect to say "_... is passed by reference_". Pass-by-reference semantics is well-defined and it is not what happens here. – MAV Jun 17 '14 at 19:07
  • 1
    @DavidCrowell Your first sentance, `DataTable, and anything else derived from object is passed by reference.` is wrong. Your second sentence is not wrong, but your first is. – Servy Jun 17 '14 at 19:26