Store reference to array as variable

I want to have a class that can store references to arrays in a different class and be able to modify those arrays by the new arrays.

public class test : MonoBehaviour
{
    // Variables
    //,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,
	
	test2 testing;
	int[] original;

	void Start()
	{
		original = new int[] { 1, 2, 3, 4 };

		testing = new test2();
		testing.reference = original;
		testing.Change(); // changes original values
		testing.Modify(); // does not modify original

		DebugArray( original );
		Debug.Log( ":::" );
		DebugArray( testing.reference );
	}

	void DebugArray( int[] array )
	{
		for ( int i = 0; i < array.Length; ++i )
		{
			Debug.Log( array *);*
  •  }*
    
  • }*
    }

public class test2
{

  • public int reference;*

  • public void Change()*

  • {*

  •  reference[0] = 135134;*
    
  • }*

  • public void Modify()*

  • {*

  •  reference = new int[] { 4, 3, 2, 1, 0 };*
    
  • }*
    }
    The problem I’m running into with this code is that I can change the values of the original array via the new class, but I can’t modify the array. Is there a way I could do this in C#? It seems like a serious limitation of C# if I can’t. Are there any alternatives? Maybe using IntPtr? To be clear I need to be able to change the size of the array in the new class as well as the values.

The right answer would be to think real hard about what it is you’re trying to do, and then think real hard about a better way to do it.

To change the array referenced by a variable you need access to that variable. This is tru in C or C# or any other language.

public class test : MonoBehaviour
 {
     // Variables
     //,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,
     
     test2 testing;
     public int[] original;
 
     void Start()
     {
         original = new int[] { 1, 2, 3, 4 };
 
         testing = new test2();
         testing.reference = original;
         testing.Change(); // changes original values
         testing.Modify(); // does not modify original
 
         DebugArray( original );
         Debug.Log( ":::" );
         DebugArray( testing.reference );
     }
 
     void DebugArray( int[] array )
     {
         for ( int i = 0; i < array.Length; ++i )
         {
             Debug.Log( array *);*

}
}
}

public class test2
{
test1 theOtherInstance;

public void Change()
{
theOtherInstance.original[0] = 135134;
}

public void Modify()
{
theOtherInstance.original = new int[] { 4, 3, 2, 1, 0 };
}
}