private static Array CreateSized<T>(Array sourceArray)
int[] shape = new int[sourceArray.Rank];
for (int i = 0; i < shape.Length; i++)
shape[i] = sourceArray.GetLength(i);
return Array.CreateInstance(typeof(T), shape);
private static void OutputArrayInfo(string name, Array sourceArray)
Console.Write($"{name} Rank: {sourceArray.Rank}, Type:{ sourceArray.GetType()}, Size: ");
for (int i = 0; i < sourceArray.Rank; i++)
Console.Write($"{i}/{sourceArray.GetLength(i)}, ");
public static void Main()
int [] a = { 1, 2, 4, 8 };
int [,] b = { { 10, 20, 40, 80 }, {100, 200, 400, 800 } };
var fa = (float[]) CreateSized<float>(a);
OutputArrayInfo(nameof(a), a);
OutputArrayInfo(nameof(fa), fa);
var fb = (decimal[,])CreateSized<decimal>(b);
OutputArrayInfo(nameof(b), b);
OutputArrayInfo(nameof(fb), fb);
fb[0,2] = b[0, 2] * 3.14m;
fb.SetValue((decimal)b[0, 3] * 3.14m, 0, 3);
Console.WriteLine($"b[0,2]: {b[0,2]} / {b.GetValue(0, 2)}; b[0,3]: {b[0,3]} / {b.GetValue(0, 3)}");
Console.WriteLine($"fb[0,2]: {fb[0,2]} / {fb.GetValue(0, 2)}; fb[0,3]: {fb[0,3]} / {fb.GetValue(0, 3)}");
dynamic fc = CreateSized<double>(b);
fc[0, 2] = b[0, 2] * 3.141592;
Console.WriteLine($"fc[0, 2]: {fc[0, 2]}");
Console.WriteLine(fc[0, 2]);