DataGridView:按值传递还是按引用传递?

发布于 2024-08-11 16:39:34 字数 69 浏览 9 评论 0原文

我认为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落叶缤纷 2024-08-18 16:39:34

从内存的角度来看,通过值或引用传递它并不重要,因为 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.

策马西风 2024-08-18 16:39:34

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.

南风起 2024-08-18 16:39:34

要添加约瑟夫的答案,所有按值传递的操作都是在被调用方法堆栈帧中的调用堆栈上创建一个新变量,并将 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.

不…忘初心 2024-08-18 16:39:34
  1. 您不会(需要)经常传递任何 Control
  2. 您根本无法传递对象本身。
  3. 您只能传递对对象的引用,并且按值(默认)或按引用(ref 参数)传递对内存使用没有影响。这是一个设计决策,但通常,对于控件来说,您将按值传递引用。
  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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文