简体   繁体   中英

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 :)

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM