Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are event arguments passed by reference or value in C#?

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.

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.

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?

like image 515
Xantham Avatar asked Aug 03 '12 17:08

Xantham


People also ask

Does C pass by reference or value?

C always uses 'pass by value' to pass arguments to functions (another term is 'call by value', which means the same thing), which means the code within a function cannot alter the arguments used to call the function, even if the values are changed inside the function.

Are arguments passed by reference in C#?

In C#, arguments can be passed to parameters either by value or by reference. Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment.

What is pass by value and pass by reference in C programming?

An algorithm is given below to explain the working of pass by value in C language. START Step 1: Declare a function with pointer variables that to be called. Step 2: Declare variables a,b. Step 3: Enter two variables a,b at runtime. Step 4: Calling function with pass by reference.

How are arguments passed by value or by reference?

When you pass an argument by reference, you pass a pointer to the value in memory. The function operates on the argument. When a function changes the value of an argument passed by reference, the original value changes. When you pass an argument by value, you pass a copy of the value in memory.


2 Answers

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.

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

EDIT: 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.

like image 138
Jon Skeet Avatar answered Sep 27 '22 20:09

Jon Skeet


Arguments themselves are passed by value by default. However, depending on their type, they can be values or references to the actual values you're working with.

Note that this is not the same as what is commonly known as passing by reference, as the very value actually passed to an argument is copied (i.e. passed by value). However, the effect is similar in that if you change the referenced object within the method, the changes will be visible outside of the method (in the code where you invoked the method), too.

Now, when passing by value, there is nothing special about event arguments; whether the values are copied or only their references entirely depends on their type. So, as you said, int and long arguments (and some more, any struct types) are value types, while others like string (and any class instances) are reference types.

Note that a true passing by reference is possible in C#, too, but that requires the ref keyword.

like image 39
O. R. Mapper Avatar answered Sep 27 '22 18:09

O. R. Mapper