Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:20:47+00:00 2026-06-14T04:20:47+00:00

As far as i had understood, when you use a reference type as a

  • 0

As far as i had understood, when you use a reference type as a parameter in a method, the value on the stack, is copied and the formal parameter therefore points to the same memory address, on the heap, as the original, hence changes are persisted once you have finished with the method.

How does this work with tasks? I have just created 2 new tasks and passed in an array which was declared on the UI thread. Changes made in one of the new tasks were immediately shown in the second task. When i try to change the input(the array) via the UI thread, the same parameter hasnt changed on the 2 new tasks. I was under the impression it should have since they should all be pointing at the same memory location on the heap??

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace TasksAndMemory
{
    class Program
    {
        private static ManualResetEvent mre = new ManualResetEvent(false);

        static void Main(string[] args)
        {
            int[] readToEnd = new int[2];
            int[] data = new int[] { 1, 2, 3, 4, 5, 6 };
            int valueType = 5;


            int pageCounter = 1;


            Task[] tasks = new Task[2];


            for (int x = 1; x < 3; x++)
            {
                //Needed due to closure problem
                int i = x;

                tasks[i-1] = Task.Factory.StartNew(() =>
                {
                    SpecialMethod(data, readToEnd, i, valueType);
                });
            }

            while(pageCounter < 4)
            {
                if (readToEnd[0] == 1 && readToEnd[1] == 1)
                {
                    //Sets the state of the event to nonsignaled, causing threads to block
                    mre.Reset();
                    int[] temp = new int[] { 7, 8, 9, 10, 11, 12 };
                    data = temp;
                    readToEnd[0] = 0;
                    readToEnd[1] = 0;

                    //Sets the state of the event to signaled, allowing one or more waiting threads to proceed.
                    mre.Set();
                    pageCounter++;
                }
            }
            Console.ReadLine();
        }

        public static void SpecialMethod(int[] historicalData, int[] readToEnd, int taskNumber, int valueTy)
        {
            int[] temp = new int[] { 100, 200, 300, 400, 500, 600 };

            for (int x = 0; x <= historicalData.Length; x++)
            {
                if (x == historicalData.Length)
                {
                    readToEnd[taskNumber-1] = 1;
                    mre.WaitOne();
                    x = 0;
                 }
                else
                {
                    valueTy++;
                    temp[x] = temp[x] + taskNumber;
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:20:48+00:00Added an answer on June 14, 2026 at 4:20 am

    Your analysis seems correct at the start, but your conclusions are not.

    You have a reference type (an array) and you pass it to a method by value (which is the default). This means that the reference to that array, which sits on the heap, is copied.

    Because both the variable in SpecialMethod and Main have the same reference, changing the value that they reference will be “seen” by both variables.

    That only applies if you mutate the array. That’s what you do with readToEnd, which is why the sections of code dealing with it work as you intended.

    With data on the other hand you don’t mutate the array, you just assign a new array to the variable. That’s changing the reference, not the object that it references, and that’s why you’re having problems.

    As for solutions, there are several. First, you could change the code to mutate the array rather than assigning a new one; just change the exising values. If you need to change the number of elements consider using a List rather than an array.

    Another option is, rather than passing an array, is to add another layer of indirection. You could make a new class that has a property which is an array, pass an object of that type to SpecialMethod, and then you can change that object’s property and see it reflected in both locations. You could use something like this to cover the general case:

    public class Wrapper<T>
    {
        public T Value { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been updating an Android app today which so far had a single TableLayout
I'm using Symfony 1.4 and Doctrine. So far I had no problem running tasks
our svn had so far traditional layout: trunk branches/ tags/ i cloned whole the
So far I have been creating Web Portal but recently I had a request
I have had no luck with this task so far so grateful for any
I've had a login I have used so far to login while I was
In the pages so far, I have had used one connection class and one
I have tried several things so far, but I haven't had much luck yet.
In Delphi 2009, I had no major problems with Generics so far (using Generics.Collections
I've tried in 2 forums, but I had no luck so far. So, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.