JS Array to C# Array

Ok, I am making the transition from JS (UnityScript) to C#, and I am wanting to know a quick and easy solution with this Array issue of mine. I am trying to convert this Array:

var someVar : Transform[] = [];

and

var someOtherVar : MyClass[] = [];

I have tried this:

public List<MyClass> someOtherVar = new List.<MyClass>();

But to no avail. In some cases I try this when I create a property for a class, not just for a local variable inside a function. This is the error I am getting: “error CS1519: Unexpected symbol `<’ in class, struct, or interface member declaration”

Please help with this, I know its something small I am missing. Thanks in advance.

(I am using this for referece Arrays)

You’re using a builtin array in JavaScript which is also available in C#. There’s no reason to create a List (which doesn’t have the . in C#)

public Transfrm[] someVar;

public MyClass[] someOtherVar;

Oh gosh, I feel silly now, ha.

So how do I set an initial value for it? In UnityScript:

var someVar[] = [];

But if I do this in C#:

public someVar[] = [];

I get an error “error CS1519: Unexpected symbol `[’ in class, struct, or interface member declaration”

Or do I not have to do this (adding square brackets)?

Again, sorry for asking such noobish question on this.

You don’t have to do that in either language. They’re empty by default.

You’re not providing a type in your C# version. There are a few initialization methods you can use.

// uninitialized array
public int[] MyInts;

// initialized to contain 10 null values
public MyClass[] Classes = new MyClass[10];

// initialized and given values
public int[] MyInts = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

c#

Transform[] someVar = {}; // someVar is array with zero elements
//or
Transform[] someVar = new Transform[0]; // same as above
//or just
Transform[] someVar; // somevar is null



MyClass[] someOtherVar = {};
// or
MyClass[] someOtherVar = new MyClass[0];
// or even
List<MyClass> someOtherVar = new List<MyClass>();

Thanks for the replies. It helped me move forward, but I have come across something else now that I can’t seem to figure out.

How do I add and remove values to the array? In UnityScript, to add to an array I did:

var someVar : myClass[] = [];
someVar += [new myClass()];

…and to remove from the array was (I converted it to a normal array):

var arr : Array = new Array(someVar);
arr.RemoveAt(index);
myClass = arr.ToBuiltin(myClass);

But C# doesn’t have the Array class does it? Asking one last time for help on this :slight_smile:

List<MyClass> someVar = new List<MyClass>();

// adding value:
someVar.Add(new MyClass());
// removing first value:
someVar.RemoveAt(0);
// removing last value:
someVar.RemoveAt(someVar.Count-1);

Thank you! That does it!