private static Array innerConvertToNew <SourceLeafType, TargetLeafType>( int depth, Type[] types, Array sourceArray, ElementConverter <SourceLeafType, TargetLeafType> converter) { bool atLeaf = (++depth == types.Length - 1); int[] dims = GetMultiDimensions(sourceArray); Array targetJaggedArray = Array.CreateInstance(types[depth], dims); switch (sourceArray.Rank) { case 1: if (atLeaf) { for (int i = 0; i < sourceArray.Length; i++) { targetJaggedArray.SetValue(converter((SourceLeafType)sourceArray.GetValue(i)), i); } } else { Array[] src = (Array[])sourceArray; Array[] tgt = (Array[])targetJaggedArray; for (int i = 0; i < src.Length; i++) { tgt[i] = innerConvertToNew(depth, types, src[i], converter); } } break; case 2: if (atLeaf) { for (int i = 0; i < sourceArray.GetLength(0); i++) { for (int j = 0; j < sourceArray.GetLength(1); j++) { targetJaggedArray.SetValue(converter((SourceLeafType)sourceArray.GetValue(i, j)), i, j); } } } else { Array[,] src = (Array[, ])sourceArray; Array[,] tgt = (Array[, ])targetJaggedArray; for (int i = 0; i < src.GetLength(0); i++) { for (int j = 0; j < src.GetLength(1); j++) { tgt[i, j] = innerConvertToNew(depth, types, src[i, j], converter); } } } break; default: throw new ArgumentException("Multidimensional arrays of greater than rank 2 are not supported"); } return(targetJaggedArray); }
static void Main1() { int L1 = 0; Array[,] L2 = null; double L3 = 0.0; object L4 = null; Method1(ref L1, ref L2, ref L3, ref L4); }
private static void innerConvertElements2( int depth, int leafDepth, Array targetArray, Array sourceArray, ElementConverter2 converter) { bool atLeaf = (++depth == leafDepth); switch (targetArray.Rank) { case 1: if (atLeaf) { for (int i = 0; i < targetArray.Length; i++) { targetArray.SetValue(converter(targetArray.GetValue(i), sourceArray.GetValue(i)), i); } } else { Array[] ja1 = (Array[])targetArray; Array[] ja2 = (Array[])sourceArray; for (int i = 0; i < ja1.Length; i++) { innerConvertElements2(depth, leafDepth, ja1[i], ja2[i], converter); } } break; case 2: if (atLeaf) { for (int i = 0; i < targetArray.GetLength(0); i++) { for (int j = 0; j < targetArray.GetLength(1); j++) { targetArray.SetValue(converter(targetArray.GetValue(i, j), sourceArray.GetValue(i, j)), i, j); } } } else { Array[,] ja1 = (Array[, ])targetArray; Array[,] ja2 = (Array[, ])sourceArray; for (int i = 0; i < ja1.GetLength(0); i++) { for (int j = 0; j < ja1.GetLength(1); j++) { innerConvertElements2(depth, leafDepth, ja1[i, j], ja2[i, j], converter); } } } break; default: throw new ArgumentException("Multidimensional arrays of greater than rank 2 are not supported"); } }
private static void innerConvertElements( int depth, int leafDepth, Array jaggedArray, ElementConverter converter) { bool atLeaf = (++depth == leafDepth); switch (jaggedArray.Rank) { case 1: if (atLeaf) { for (int i = 0; i < jaggedArray.Length; i++) { jaggedArray.SetValue(converter(jaggedArray.GetValue(i)), i); } } else { Array[] ja = (Array[])jaggedArray; for (int i = 0; i < ja.Length; i++) { innerConvertElements(depth, leafDepth, ja[i], converter); } } break; case 2: if (atLeaf) { for (int i = 0; i < jaggedArray.GetLength(0); i++) { for (int j = 0; j < jaggedArray.GetLength(1); j++) { jaggedArray.SetValue(converter(jaggedArray.GetValue(i, j)), i, j); } } } else { Array[,] ja = (Array[, ])jaggedArray; for (int i = 0; i < ja.GetLength(0); i++) { for (int j = 0; j < ja.GetLength(1); j++) { innerConvertElements(depth, leafDepth, ja[i, j], converter); } } } break; default: throw new ArgumentException("Multidimensional arrays of greater than rank 2 are not supported"); } }
static void Method1(ref int a, ref Array[,] b, ref double c, ref object d) { try { sbyte[,] aa = new sbyte[10, 10]; aa[0, a] += su; aa[a, a] += su; } catch (IndexOutOfRangeException) { b[a, a] = null; } }
// NOT TESTED! WILL IT WORK FOR ANY GIVEN ARRAY, SAY OfType.'LABEL'? public Array[] TwoDim_to_OneDim_Array(Array [,] inp) { int n = 0; Array [] ans = new Array[n]; for (int i = 0; i < inp.GetLength(0); i++) { for (int j = 0; j < inp.GetLength(1); j++) { ans[n] = inp[i, j]; n++; } } return(ans); }
static void getSum(Array[,] array) { Int32 sum = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (j == i) { ///string b = (array[i, j]); Console.Write(array[i, j] + " "); var b = array.GetValue(i, j); sum = sum + (int.Parse(b.ToString())); } else { Console.Write("x "); } } Console.WriteLine(); } Console.WriteLine($"The sum of the diagonal is {sum}"); }