Si alguien todavía está buscando una posible solución. Jugar poco con genéricos es posible si envuelve cada conjunto en clase para que pueda asignarse a otra instancia de la misma clase de ajuste haciendo referencia a un conjunto en él.
Pero esto debe usarse solo como prueba de concepto que es posible hacerlo. En general, no recomendaría el uso de esto, pero sugeriría rediseñar el código de una manera más eficiente. También vale la pena mencionar que simplemente puede asignar matriz a otra matriz como referencia a sus elementos (DOH).
Dicho esto aquí es el código de ejemplo de la matriz de referencia genérica a la matriz de datos:
using System;
using System.Diagnostics;
public class RefArray<TElem>
{
TElem[] data;
/// <summary>Initializes RefArray by creating Ref<T>[]data from values.</summary>
public RefArray(TElem[] values)
{
data = values;
}
/// <summary>Creates reference RefArray pointing to same RefArray<T> data.</summary>
public RefArray(RefArray<TElem> references)
{
this.data = references.data;
}
public int Length
{
get { return data.Length; }
}
public TElem this[int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
}
public static class RefArrayTest
{
public static void Usage()
{
// test ints (struct type)
RefArray<int> c = new RefArray<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
RefArray<int> d = new RefArray<int>(c);
Debug.Print(string.Format("c[3]: {0,-30}, d[3]: {1}", c[3], d[3]));
c[3] = 1111;
Debug.Print(string.Format("c[3]: {0,-30}, d[3]: {1}", c[3], d[3]));
d[3] = 2222;
Debug.Print(string.Format("c[3]: {0,-30}, d[3]: {1}", c[3], d[3]));
d[3] = c[3] + 3333;
Debug.Print(string.Format("c[3]: {0,-30}, d[3]: {1}", c[3], d[3]));
// test strings (class type)
RefArray<string> a = new RefArray<string>(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" });
RefArray<string> b = new RefArray<string>(a);
Debug.Print(string.Format("a[3]: {0,-30}, b[3]: {1}", a[3], b[3]));
a[3] = "set a";
Debug.Print(string.Format("a[3]: {0,-30}, b[3]: {1}", a[3], b[3]));
b[3] = "set b";
Debug.Print(string.Format("a[3]: {0,-30}, b[3]: {1}", a[3], b[3]));
a[3] = b[3] + " + take b set a";
Debug.Print(string.Format("a[3]: {0,-30}, b[3]: {1}", a[3], b[3]));
// proof of no point since
string[] n1 = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
string[] n2 = n1;
Debug.Print(string.Format("n1[3]: {0,-30}, n2[3]: {1}", n1[3], n2[3]));
n1[3] = "set n1";
Debug.Print(string.Format("n1[3]: {0,-30}, n2[3]: {1}", n1[3], n2[3]));
n2[3] = "set n2";
Debug.Print(string.Format("n1[3]: {0,-30}, n2[3]: {1}", n1[3], n2[3]));
n1[3] = n2[3] + " + take n2 set n1";
Debug.Print(string.Format("n1[3]: {0,-30}, n2[3]: {1}", n1[3], n2[3]));
}
}
Además, si los elementos mencionados tienen que estar fuera de servicio se podría añadir la clase Ref_T genérica para envolver cada valor como referencia:
using System;
using System.Text;
using System.Diagnostics;
public class Ref_T<TValue>
{
public TValue data;
public Ref_T(TValue value)
{
this.data = value;
}
}
public class RefArray<TElem>
{
public readonly Ref_T<TElem>[] data;
/// <summary>Initializes RefArray by creating Ref<T>[]data from values.</summary>
public RefArray(TElem[] values)
{
data = new Ref_T<TElem>[values.Length];
for (int i = 0; i < values.Length; i++)
{
// creates reference members
data[i] = new Ref_T<TElem>(values[i]);
}
}
/// <summary>Creates reference RefArray pointing to same RefArray<T> data.</summary>
public RefArray(RefArray<TElem> references)
{
data = references.data;
}
/// <summary>Creates reference RefArray pointing to same Ref<T>[] references.</summary>
public RefArray(Ref_T<TElem>[] references)
{
data = references;
}
public int Length
{
get { return data.Length; }
}
public TElem this[int index]
{
get { return data[index].data; }
set { data[index].data = value; }
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
int count = data.Length;
for (int i = 0; i < count; i++)
sb.Append(string.Format("[{0}]:{1,-4}, ", i, data[i].data));
return sb.ToString();
}
public static implicit operator Array(RefArray<TElem> a)
{
return a.data;
}
}
public static class RefArrayTest
{
public static void Usage()
{
// test ints (struct type) out of order
// initialize
RefArray<int> e = new RefArray<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
// reference e out of order
RefArray<int> f = new RefArray<int>(new Ref_T<int>[]
{ e.data[8], e.data[6], e.data[4], e.data[2], e.data[0],
e.data[9], e.data[7], e.data[5], e.data[3], e.data[1]
});
Debug.WriteLine("Initial: ");
Debug.WriteLine("e: [" + e + "]");
Debug.WriteLine("f: [" + f + "]\r\n");
e[3] = 1111;
Debug.WriteLine("e[3] = 1111;");
Debug.WriteLine("e: [" + e + "]");
Debug.WriteLine("f: [" + f + "]\r\n");
f[3] = 2222;
Debug.WriteLine("f[3] = 2222;");
Debug.WriteLine("e: [" + e + "]");
Debug.WriteLine("f: [" + f + "]\r\n");
f[3] = e[3] + 3333;
Debug.WriteLine("f[3] = e[3] + 3333;");
Debug.WriteLine("e: [" + e + "]");
Debug.WriteLine("f: [" + f + "]\r\n");
Array.Reverse(f);
Debug.WriteLine("Array.Reverse(f);");
Debug.WriteLine("e: [" + e + "]");
Debug.WriteLine("f: [" + f + "]\r\n");
}
}
salidas:
Initial:
e: [[0]:0 , [1]:1 , [2]:2 , [3]:3 , [4]:4 , [5]:5 , [6]:6 , [7]:7 , [8]:8 , [9]:9 , ]
f: [[0]:8 , [1]:6 , [2]:4 , [3]:2 , [4]:0 , [5]:9 , [6]:7 , [7]:5 , [8]:3 , [9]:1 , ]
e[3] = 1111;
e: [[0]:0 , [1]:1 , [2]:2 , [3]:1111, [4]:4 , [5]:5 , [6]:6 , [7]:7 , [8]:8 , [9]:9 , ]
f: [[0]:8 , [1]:6 , [2]:4 , [3]:2 , [4]:0 , [5]:9 , [6]:7 , [7]:5 , [8]:1111, [9]:1 , ]
f[3] = 2222;
e: [[0]:0 , [1]:1 , [2]:2222, [3]:1111, [4]:4 , [5]:5 , [6]:6 , [7]:7 , [8]:8 , [9]:9 , ]
f: [[0]:8 , [1]:6 , [2]:4 , [3]:2222, [4]:0 , [5]:9 , [6]:7 , [7]:5 , [8]:1111, [9]:1 , ]
f[3] = e[3] + 3333;
e: [[0]:0 , [1]:1 , [2]:4444, [3]:1111, [4]:4 , [5]:5 , [6]:6 , [7]:7 , [8]:8 , [9]:9 , ]
f: [[0]:8 , [1]:6 , [2]:4 , [3]:4444, [4]:0 , [5]:9 , [6]:7 , [7]:5 , [8]:1111, [9]:1 , ]
Array.Reverse(f);
e: [[0]:0 , [1]:1 , [2]:4444, [3]:1111, [4]:4 , [5]:5 , [6]:6 , [7]:7 , [8]:8 , [9]:9 , ]
f: [[0]:1 , [1]:1111, [2]:5 , [3]:7 , [4]:9 , [5]:0 , [6]:4444, [7]:4 , [8]:6 , [9]:8 , ]
Espero que esto ayude a alguien.
Su pregunta parece confundir las referencias con punteros, y utiliza algunos términos bastante confusos como "los contenidos" de alguna variable y "variable en sí misma". 'int * p = & i [5]' asigna la * dirección * del quinto elemento de la matriz 'i' a la variable' p', que se declara como un puntero a 'int'. Las referencias no son lo mismo que los punteros. No estoy seguro acerca de C#, pero desde una perspectiva de C++ no está claro lo que estás preguntando. –
@JamieBullock - Creo que es bastante claro, ya que todos los demás entendieron de lo que estaba hablando. Las referencias en C++ son más o menos lo mismo que los punteros, excepto que no se pueden hacer cálculos matemáticos o reasignarlos. De todos modos, las referencias C# no son las mismas que las referencias C++, y C# no tiene un concepto de punteros (cuando no se usa código inseguro), por lo que las referencias son las que usarías en C# ya que no hay nada más. Además, dije "instancia de la variable en sí". –