Problem passing a structure by reference

zequion 446 Reputation points
2024-06-24T04:37:43.77+00:00

I have a class and in one of the functions Fcn_Task_Async() I have a parameter that is a structure called "ref StTask_Async".

Inside the function, I need to create a Task passing "ref StTask_Async" to assign values ​​in the Fcn_Task_Async_Step2() function. However, Startnew does not allow me to use "ref StTask_Async" because it shows the error "CS1628: Cannot use in ref or out parameter inside an anonymous method, lambda expression, or query expression" and that forces me to use Name_Task_Async.St_Task_Async StTask_Async_ = StTask_Async ; to be able to pass "ref StTask_Async_".

My question: The strange thing is that, in the return, "StTask_Async" has the correct values ​​assigned to "StTask_Async_". I don't understand.

private Fcn_Task_Async(ref Name_Task_Async.St_Task_Async StTask_Async)

{

// Startnew forces me to use a local variable

Name_Task_Async.St_Task_Async StTask_Async_ = StTask_Async;

// Runs the task where it assigns variables to the local structure
StTask_Async_.

StTask_Async.StTask_Async_Tasks[Task_index_].Task = System.Threading.Tasks.Task.Factory.StartNew<Name_Task_Async.St_Task_Async_Task>(() => Name_Task_AsyncAdd.Cls_Task_AsyncAdd.Fcn_Task_Async_Step2(ref StTask_Async_);

// Wait for StartNew completion.

SLEEP(10000);

// AT THIS POINT, if I query the values ​​​​of "StTask_Async" they are those assigned in the Task to the local structure. Why?

}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,030 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 46,556 Reputation points Microsoft Vendor
    2024-06-24T08:20:10.01+00:00

    Hi @zequion , Welcome to Microsoft Q&A,

    The behavior you observe is due to the way value types (such as structures) and reference types (such as classes) are handled in C#. When you pass a structure by value, you are passing a copy of the structure. However, when you pass a structure by reference (using ref), you are passing a reference to the original structure, allowing modifications to affect the original structure.

    private void Fcn_Task_Async(ref Name_Task_Async.St_Task_Async StTask_Async)
    {
        // Create a local copy of the original struct
        Name_Task_Async.St_Task_Async StTask_Async_ = StTask_Async;
    
        // Start a new Task to modify the local copy
        StTask_Async.StTask_Async_Tasks[Task_index_].Task = System.Threading.Tasks.Task.Factory.StartNew(() =>
        {
            Name_Task_AsyncAdd.Cls_Task_AsyncAdd.Fcn_Task_Async_Step2(ref StTask_Async_);
        });
    
        // Wait for the Task to complete
        System.Threading.Tasks.Task.WaitAll(StTask_Async.StTask_Async_Tasks[Task_index_].Task);
    
        // After the Task completes, assign the modified local copy back to the original struct
        StTask_Async = StTask_Async_;
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.