Listing 3

using namespace System;
using namespace System::Threading;

public ref struct ArrayManip
{
  static int TotalValues(array<int>^ array)
  {
/*1*/   int sum = 0;
/*2*/   Monitor::Enter(array);
    {
      for (int i = 0; i < array->Length; ++i)
      {
        sum += array[i];
      }
    }
    Monitor::Exit(array);
    return sum;
  }

  static void SetAllValues(array<int>^ array, int newValue)
  {
/*3*/   Monitor::Enter(array);
    {
      for (int i = 0; i < array->Length; ++i)
      {
        array[i] = newValue;
      }
    }
    Monitor::Exit(array);
  }

  static void CopyArrays(array<int>^ array1, array<int>^ array2)
  {
/*4*/   Monitor::Enter(array1);
    {
/*5*/     Monitor::Enter(array2);
      {
        Array::Copy(array1, array2, 
          array1->Length < array2->Length ? array1->Length
          : array2->Length);
      }
      Monitor::Exit(array2);
    }
    Monitor::Exit(array1);
  }
};