Take a look at the following example:
public void inc(int num) {
num++;
}
int a = 5;
inc(a);
In this case, inc won't increment the a variable itself. It is not pointing to the same location in the memory. In order to change a, I'll have to use ref.
However, in this example
public static void ExportSelectedRow(GridView gridView, object toObject, int skipCols)
{
GridViewRow gridViewRow = gridView.SelectedRow;
if (toObject is DataTable)
{
DataTable returnDt = (DataTable)toObject;
GridViewColsToDatatable(gridView, returnDt, skipCols);
DataRow dr = returnDt.NewRow();
for (int i = skipCols; i < gridViewRow.Cells.Count; i++)
dr[i - skipCols] = gridViewRow.Cells[i].Text;
returnDt.Rows.Add(dr);
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
DataTable dt = new DataTable();
GridViewHelper.ExportSelectedRow(GridView1, dt, 1);
...
}
the selected row will be exported from the GridView1 into DataTable although I didn't even referenced it in the function. So the toObject will be updated. It seems that
DataTable returnDt = (DataTable)toObject;
is actually referencing to the toObject. So my question, why in this example it is a reference, but in the first one, it is not?