Question

I think of DataGridView's as being memory hogs. Is it better to pass by value a datagridview or by reference? Should it even be passed at all?

Was it helpful?

Solution

Passing it by value or reference isn't going to matter from a memory stand point, because the DataGridView is a reference type, not a value type.

OTHER TIPS

The type DataGridView is a reference type and hence it's not possible to pass the object by value. You can pass the reference to the object by value but that is a very small (typically pointer sized) value.

To add to Joseph's answer, All passing it by value does is create a new variable on the call stack in the called methods stack frame, and copy the address (in the Heap) of the DataGridView object into that variable for use by the called method. All this does is prevent the called method from assigning a new DataGridView object's address to the variable in the caller (calling method), and thus changing which dataGridView the caller will be pointing to.

  1. You won't (need to) pass any Control very often
  2. You cannot pass the object itself at all.
  3. You can only pass the reference to an object, and doing so by value (the default) or by reference (ref parameter) has no impact on memory usage. It is a design decision but usually, certainly for Controls, you will pass the reference by value.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top