Search code examples
javac#arraylistcode-translation

Converting some Java to C#


This is the line of code I am having trouble converting:

Vector3 n1 = m.normals.get((int) face.normal.X - 1);

I am not sure what this translates to in c# as I have tried quite a few things. I think it may also be due to a problem with my lists:

class Model
{
    public List<Vector3> vertices = new List<Vector3>();
    public List<Vector3> normals = new List<Vector3>();
    public List<Face> faces = new List<Face>();

}

They were supposed to be:

class Model
{
    public List<Vector3> vertices = new ArrayList<Vector3>();
    public List<Vector3> normals = new ArrayList<Vector3>();
    public List<Face> faces = new ArrayList<Face>();

}

I dont know what ListArray translates to in c# either.

Any help would be greatly appreciated :)


Solution

  • Java               C#
    ------------    --------
    List<T>      is IList<T>   // Interface
    ArrayList<T> is List<T>    // Class implementing the interface
    

    You can translate your code like this:

    class Model
    {
        public IList<Vector3> vertices = new List<Vector3>();
        public IList<Vector3> normals = new List<Vector3>();
        public IList<Face> faces = new List<Face>();
    }
    

    Java's get becomes C#'s indexer, so

    Vector3 n1 = m.normals.get((int) face.normal.X - 1);
    

    becomes

    Vector3 n1 = m.normals[(int)face.normal.X - 1];