grjj3
grjj3

Reputation: 381

C# pointer issue

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?

Upvotes: 0

Views: 1133

Answers (5)

Chad
Chad

Reputation: 19609

C# has parameter types. Value Types (structs, enums, numbers, etc) and Reference Types (objects, strings, etc).

In your first example the value 5 is passed to the function, but in the second example GridView1's reference is passed.

See MDN for a list of value-types and reference-types which define how parameters are passed.

EDIT

For a better definiton please see harold's answer.

Upvotes: 6

Mark Byers
Mark Byers

Reputation: 838216

DataTable is a reference type. int is a value type.

C# passes by value (unless ref or out is used).

  • When you pass a DataTable to a method, you are actually passing a copy of the reference. Both references refer to the same object. Modifications to the object will be visible to the caller.
  • When you pass an int you're passing a copy of the value.

If you use the ref keyword, it will pass by reference instead.

Upvotes: 3

Ethan
Ethan

Reputation: 516

The difference here is between a base type such as an int or double and an object. When you pass an object you are passing a reference to it, while with intrinsic types yo are passing a copy of the value to the function.

structs are interesting in that they syntactically look like classes but are passed by value (copied) instead of by reference.

Upvotes: 0

user555045
user555045

Reputation: 64904

Contrary to what other people are going to say, by default everything (yes also reference types) in C# is passed by value. With ref or out they aren't.
But in the case of reference types, the thing that is passed by value is a reference.

int is of course a value type, DataTable is a reference type.

Upvotes: 12

Vilx-
Vilx-

Reputation: 106920

There are two types of ermm... types in .NET: Value types and reference types. The difference is exactly what you've demonstrated above.

Basically every struct is a value type and every class is a reference type. Basic types (like int, decimal, datetime, byte, etc.) are also implemented as structs, so they are value types. Well, actually, they're a bit special, but they are quite like structs.

MSDN has an article about this, and the topic has been extensively covered in other places too. Just google for "value types vs reference types".

Upvotes: 1

Related Questions