如何通过引用真正在C#中工作? [英] How does passing by reference really work in C#?

查看:96
本文介绍了如何通过引用真正在C#中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何通过引用传递在C#中工作。如果我通过引用一个函数传递一个WPF DataGrid控件,并且慢慢地将这个函数添加到它的DataGrid.Items集合中,UI中的DataGrid会随着每个新的DataGrid.Items.Add()调用而更新吗?或者只会在函数返回时更新?我想实现前一种情况,所以如果按照引用传递并不能解决问题,我会很乐意提供一些关于如何实现这一点的建议。提前致谢。

I am wondering how passing by reference works in C#. If I pass a WPF DataGrid control by reference to a function, and slowly have the function add items to its DataGrid.Items collection, will the DataGrid in the UI update with each new DataGrid.Items.Add() call? Or will it only update when the function returns? I would like to achieve the former scenario, so if passing by reference doesn't do the trick, I would love some advice on how to accomplish this. Thanks in advance.

推荐答案

无论如何,通过引用传递与您的情况无关。通过引用传递(即 ref )可以让您分配给调用范围中的变量。你并没有试图这样做。您只想访问 DataGrid 引用并访问它的 Items 字段。这根本不需要 ref

Passing by reference is irrelevant to your situation anyway. Passing by reference (i.e. ref) allows you to assign to the variable in the calling scope. You are not trying to do that. You only want to access the DataGrid reference and access its Items field. That does not require ref at all.

这篇关于如何通过引用真正在C#中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆