Statically typed jagged Lists in c#

My basic question is how to I type Lists where the elements are of multiple dimensions and is this even valid using Lists?

Now, more specifically - I am transcribing some kmeans clustering code into C#. I’m stuck on the following array structure:

[[[int,int,int],[[int,int,int]]], ...]

So this is a root array ‘A’ with each element containing another array ‘B’ with the first element of ‘B’ containing and array ‘C’ of integers, and the second element containing an array ‘D’ holding one element ‘E’ that is an array of integers. I know, this hurts. Does the following chart help?

A[     B[     C[int,int,int]   ,      D[    E[int,int,int]    ]     ],    ...] 

So I am trying to convert this untyped javascript array structure into a typed C# List. I’m using Lists because I have no way of knowing the potential length of the arrays.

So my first attempts at typing my List objects look something like this, which I believe is accurate up to element C.

 List<List<List<int>>>  myList

However, due to the fact that the dimensions of C and D are not the same, I’m not sure how I should type this.

Your biggest problem would be making sure that everything was initialized deep in those lists, you’d be better off defining classes with array semantics to ensure that this works;

public class Semantics
{
      public List<object> items = new List<object>();
      public virtual void Add( object value ) { items.Add(value); }
      public virtual void Remove( object value ) { items.Remove(value); }
      public virtual int Count { get { return items.Count; } }
      public object this[int index] { get { return items[index]; } set { items[index] = value; } } 
}

public class Semantics<T> : Semantics where T : class
{
     new public T this[int index] { get { return items[index] as T; } set { items[index] = value; } }
}

public class A : Semantics<B>
{
}

public class B 
{
    public C c = new C();
    public D d = new D();
   
 }

  public class C 
  {
      public int[] items = new int[3];
      public int this[int index] { get { return items[index]; } set { items[index]= value; }
      public int Count { get { return items.Length; } }
  }

 public class D : Semantics<C>
 {
 }

 public List<A> list = new List<A>();

The you can do things like this (presuming the list has these entries):

list[0][1].c[2] = 101;

Or to create some:

 list.Add(new A());

 list[0].Add(new B());
 list[0].Add(new B());
 list[0][1].d.Add(new C());
 list[0][1].d[0][1] = 202;