DataGridView: Pass by Value or Reference?

不打扰是莪最后的温柔 提交于 2019-12-12 18:12:28

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


  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.


来源:https://stackoverflow.com/questions/1750070/datagridview-pass-by-value-or-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!