事件参数是通过C#中的引用还是通过值传递的? [英] Are event arguments passed by reference or value in C#?

查看:220
本文介绍了事件参数是通过C#中的引用还是通过值传递的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个很简单的问题(我认为),但是我似乎还没有看到答案.我知道有些值是通过值传递的(如int和long),而有些值是通过引用传递的(如Strings),当您将它们传递给函数时.

A rather simple question (I think), but I don't seem to see an answer already. I know that some values are passed via value (like int and long), and others are passed by reference (like Strings) when you pass them to functions.

在我的程序中,我使用后台工作程序来执行此操作,因此当我们在后台进行长时间处理时,GUI不会锁定.我需要将数据从另一个文件传递回UI线程,因此我一直在使用事件.现在,我需要将字符串数组的列表发送回GUI线程以在那里进行处理,我担心它将如何处理.基本上,在工作线程中,我有一个循环,该循环将填充列表,通过事件处理程序将其发送回GUI,然后清除它,以便在下一次通过循环时将其填充并重新开始.

In my program, I have it using background worker so that the GUI doesn't lock up when we are doing a long process in the background. I need to pass data back to the UI thread from another file, so I have been using events for that. Now I need to send a list of arrays of Strings back to the GUI thread to handle there, and I am worried how it will be handled. Basically, in the worker thread, I have a loop that will fill up the list, send it back to the GUI via an event handler, and then clear it so it can fill it up on the next pass through the loop and start again.

我担心这样做时,如果列表是通过引用传递的,那么在UI线程上,我会认为它会在读取过程中被清除,因为工作线程仍会在后台清除它.在这种情况下,通过将是更可取的,我可以找到强制它的方法(复制到某个holder数组或添加一个互斥锁或类似的东西),但我认为最好知道是否通过事件参数传递通常是引用或值,还是与方法相同,并且将在通常传递参数时传递给它们?

I am worried that when I do this, if the list is passed by reference, then on the UI thread, I would think that it would be cleared mid-read since the worker thread will still be clearing it in the background. Passing by would be far preferable in this case, and I can find ways to force it(copy to some holder array or add a mutex or something of the sort), but I thought it would be good to know if event arguments are passed via reference or value in general, or is it just the same as methods, and it will pass them as arguments are normally passed?

推荐答案

我知道有些值是通过值传递的(如int和long),而有些值是通过引用传递的(如Strings),当您将它们传递给函数时.

I know that some values are passed via value (like int and long), and others are passed by reference (like Strings) when you pass them to functions.

不.默认情况下,所有内容都按值传递-但是,当您使用引用类型时,所有内容"都是引用.该引用按值传递.与通过引用传递不同相同.有关更多详细信息,请参见我关于参数传递的文章.

Nope. By default everything is passed by value - but when you're using reference types, the "everything" is a reference. That reference is passed by value. That's not the same as pass by reference. See my article on parameter passing for more details.

事件参数完全相同-假定相应的委托人不使用outref参数,所有引用均按值传递.

Event arguments are exactly the same - any references are passed by value, assuming the corresponding delegate doesn't use out or ref parameters.

所以要解决您的问题:是的,如果您的事件参数是可变的,并且您打算在其他线程上执行操作,则应首先创建一个副本...或者通过现有的引用然后创建工作线程中有一个新的(空)列表.

So to address your concern: yes, if your event argument is mutable and you're going to act on a different thread, you should create a copy first... or alternatively, pass the existing reference and then create a new (empty) list in your worker thread.

这篇关于事件参数是通过C#中的引用还是通过值传递的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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