Lists and Structs instead of Arrays?

Supposedly, lists and structs are preferred over arrays.

  1. Why?

  2. What if a property of your struct/subclass contains an array?

class arrayinhere{
   var gender:int;
   var CarLocations:Vector3[];
}

Lists aren’t preferred over arrays; they are used for different things. Arrays are faster, Lists are slower but can be be manipulated easily as far as adding/removing objects (but they still use arrays on the back end). If an array doesn’t need to be resized, using a List is a waste.

Saying “structs are preferred over arrays” doesn’t make any sense; structs and arrays are completely different things. You can have an array of structs, which is what an array of e.g. ints or floats or Vector3s would be.

Lists can be preferred over Arrays at times because they can be resized dynamically at run time, they have a handy list of operations that they can perform, and they can take advantage of generics.

I use arrays all the time (whenever possible). There's absolutely nothing wrong with them.

Structs (I think) are desirable because they're fast and similar to primitives: they don't have to be created with the new operator. I don't know much more about them.

I was a java buffoon before I started trying to become a Unity buffoon. To my knowledge there is nothing like a struct in java (I'm probably wrong).

Firstly the Unity team use Collections not arrays all the time. In the following examples the Lists versions are only 1.5x slower than the array version - and the Unity team use Collections. It’s not enough of a speed difference for something so unimportant to warrant writing so much extra code to use low level coding techniques like arrays. Generics gives a lot more than writing everything from scratch with arrays and the difference is not relevant compared to issues like models, textures, lighting and shaders.

Cheers.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Diagnostics;

public class DynamicMegaMod : MonoBehaviour {

private bool Exists(int val, MyClass[] search)
{
	foreach (MyClass i in search)
		if (i.MyValue == val)
			return true;
	
	return false;
}

private MyClass Find(int val, MyClass[] search)
{
	foreach (MyClass i in search)
		if (i.MyValue == val)
			return i;
	
	return null;
}	

public class MyClass 
{
	public int MyValue { get; set; }
	public string AnotherValue { get; set; }
	public string AnotherValue2 { get; set; }
}

// Use this for initialization
void Start () {		
	System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();
	
	//trying to find if an element exists in an array of 100000, 10000 times
	sw1.Start();			
	
	MyClass[] myArray = new MyClass[100000];
	
	for (int i = 0; i != 100000; i++)
		myArray *= new MyClass() {MyValue=i};*
  •   for (int i = 0; i != 10000; i++)*
    
  •   {*
    
  •   	if (Exists(50000, myArray))*
    
  •   	{*
    
  •   	}*
    
  •   }*
    
  •   sw1.Stop();					*
    
  •   System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();*
    
  •   //trying to find if an element exists in a list of 100000, 10000 times*
    
  •   sw2.Start();*
    
  •   List<MyClass> myList = new List<MyClass>(100000);*
    
  •   for (int i = 0; i != 100000; i++)*
    
  •   	myList.Add(new MyClass() {MyValue=i});				*
    
  •   for (int i = 0; i != 10000; i++)*
    
  •   {*
    
  •   	if (myList.Exists(mv => mv.MyValue == 50000))*
    
  •   	{			*
    
  •   	}*
    
  •   }		*
    
  •   sw2.Stop();	*
    
  •   System.Diagnostics.Stopwatch sw3 = new System.Diagnostics.Stopwatch();*
    
  •   //trying to find an element in an array of 100000, 10000 times*
    
  •   sw3.Start();*
    
  •   MyClass[] myArray2 = new MyClass[100000];*
    
  •   for (int i = 0; i != 100000; i++)*
    

_ myArray2 = new MyClass() {MyValue=i};_

* for (int i = 0; i != 10000; i++)*
* {*
* if (Find(50000, myArray2) != null)*
* {*
* }*
* }*

* sw3.Stop(); *

* System.Diagnostics.Stopwatch sw4 = new System.Diagnostics.Stopwatch();*

* //trying to find an element in a list of 100000, 10000 times*
* sw4.Start(); *

* List myList2 = new List(100000); *

* for (int i = 0; i != 100000; i++)*
* myList2.Add(new MyClass() {MyValue=i}); *

* for (int i = 0; i != 10000; i++)*
* {*
* if (myList2.Find(mv => mv.MyValue == 50000) != null)*
* { *
* }*
* } *

* sw4.Stop(); *
* }*

* // Update is called once per frame*
* void Update () {*

/* do something */

* }*
}

I cast a vote for IEnumerable