I have something like
var a = new object[2]
{
new double[1] { 0.5 },
new double[1] { 0.5 }
}
And I want to cast this to double[][].
I tried (double[][])a and a.Cast<double[][]>() but it didnt work
Your attempt of doing a.Cast<double[][]>() is almost correct. You can use:
double[][] b = a.Cast<double[]>().ToArray();
Explanation:
double[]s, but they are statically typed as object. To change the static type of a list's elements, you use LINQ's Cast<T>.Cast<T> takes the type of the element as T, not the type of the resulting list (this is why your attempt to use Cast<double[][]>failed). Cast<double[]> yields an IEnumerable<double[]>.IEnumerable<double[]> to an array of double[] (i.e., a double[][]), we can use LINQ's ToArray().Note that this will
object.ReferenceEquals(a, b) is false), butobject.ReferenceEquals(a[0], b[0]) is true).
a[0] = "Hello";and that would be entirely valid.