DataGridView:按值传递还是按引用传递?
我认为 DataGridView 非常消耗内存。按值传递 datagridview 还是按引用传递更好?它到底应该被通过吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从内存的角度来看,通过值或引用传递它并不重要,因为 DataGridView 是引用类型,而不是值类型。
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.
DataGridView
类型是引用类型,因此不可能按值传递对象。您可以按值将引用传递给对象,但这是一个非常小的(通常是指针大小)值。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.要添加约瑟夫的答案,所有按值传递的操作都是在被调用方法堆栈帧中的调用堆栈上创建一个新变量,并将 DataGridView 对象的地址(在堆中)复制到该变量中以供被调用方法使用。所有这一切都是为了防止被调用的方法将新的 DataGridView 对象的地址分配给调用者(调用方法)中的变量,从而更改哪个 调用者将指向的 dataGridView。
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.