views:

336

answers:

3

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?

+4  A: 

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.

Joseph
How come you can use the ref keyword to pass a datagridview? A reference of a reference? Seems strange.
0A0D
@Brian If you pass a reference type by reference, then you're passing the reference that is being used by the caller. So, for example, if you assign the parameter to a new DataGridView inside the function, the caller would see their objects become a new DataGridView, which may or may not be what you want. If you pass by value instead, then you're passing a copy of the reference, so assigning a new DataGridView to the parameter would have no effect on the caller.
Joseph
@Brian reference/value types have no relation with ref out modifiers. the modifiers are applied to variables and not the values they hold. A ref modifier on a variable that holds a reference type means that by passing that variable a copy of the reference is not made but instead the calling function will use the same variable as the caller
AZ
+1  A: 

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.

Charles Bretana
+3  A: 

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.

JaredPar