Reputation: 431
I want to be able to have multiple arrays and be able to merge them together. This concept is easy, but I also want to be able to change one of the original arrays and have the merged array change as well. The idea is that some sort of reference system would be built effectively making the merged array an array of pointers.
Example Code:
int[] a1 = new int[5];
int[] a2 = new int[5];
for (int i = 0; i < 5; i++)
{
a1[i] = i;
a2[i] = i + 5;
}
int[] a3;
a3 = MergeFunction(a1, a2)
Console.WriteLine(a3[0] + "");
a1[0] = 10;
Console.WriteLine(a3[0] + "");
Console.ReadKey();
This would output first 0, then 10
Upvotes: 1
Views: 117
Reputation: 431
In my case, I didn't actually want to use ints but another class that I had made so for the sample code, I will be using an arbitrary Class, but if someone else wants to use an int, go to this page and look at solutions for value variables. C#: Array of references / pointers to a number of integers
Firstly, I made a very simple variable class
public class Variable
{
public int a;
public int b;
public int c;
public Variable(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
Finally, here is the code to test the reference idea
static void Main(string[] args)
{
//definition of variables
Variable[] a1 = new Variable[5];
Variable[] a2 = new Variable[5];
//fill the variables
for (int i = 0; i < 5; i++)
{
a1[i] = new Variable(i, i* 2, i*3);
a2[i] = new Variable(i + 5, (i+ 5) * 2, (i+5) * 3);
}
//define combination
Variable[] a3;
//join arrays
a3 = a1.Union(a2).ToArray();
//inefficient print
Console.WriteLine(a3[0].a + "");
//change original array
a1[0].a = 10;
//inefficient print
Console.WriteLine(a3[0].a + "");
Console.ReadKey();
}
This outputs 0 then 10
Upvotes: 0
Reputation: 76
you can use Linq to solve this. For example
void Main()
{
int[] a1 = new int[5];
int[] a2 = new int[5];
for (int i = 0; i < 5; i++)
{
a1[i] = i;
a2[i] = i + 5;
}
IEnumerable<int> a3;
a3 = MergeFunction(a1, a2);
Console.WriteLine(a3.ToArray()[0] + "");
a1[0] = 10;
Console.WriteLine(a3.ToArray()[0] + "");
Console.ReadKey();
}
public IEnumerable<int> MergeFunction(int[] a1, int[] a2)
{
return a1.Union(a2);
}
Because of the deferred execution of linq it shows also the changes in the base arrays.
With this in mind you can write a small class to make the syntax for item access similar to the array syntax for item access. For example:
void Main()
{
int[] a1 = new int[5];
int[] a2 = new int[5];
for (int i = 0; i < 5; i++)
{
a1[i] = i;
a2[i] = i + 5;
}
ArrayMerger<int> a3 = new ArrayMerger<int>(a1,a2);
Console.WriteLine(a3[0] + "");
a1[0] = 10;
Console.WriteLine(a3[0] + "");
a2[0] = 15;
Console.WriteLine(a3[5] + "");
}
public class ArrayMerger<T>
{
private readonly IEnumerable<T> arrayUnion;
public ArrayMerger(T[] array1, T[] array2)
{
arrayUnion = array1.Union(array2);
}
public T this[int i]
{
get
{
return arrayUnion.ElementAt(i);
}
}
}
Upvotes: 1
Reputation: 1036
I think there's no reference system you are looking for. You have to do the merging everytime when there's a change made to the original array.
Upvotes: 0