What's the difference between an array and a list?

What’s the difference between an array and a list, they look like the same thing basically? I’m a beginner so I really don’t know the difference.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ArrayLearning : MonoBehaviour
{
    public string[] names; // a string array
    public string[] namess = new string[3]; // A string array varible thats set to 3 by default
    public string[] namesss = new string[] { "1", "2", "3", "4", "5" }; // another way of using an array

    public List<string> Names;
    public List<string> Namesss = new List<string>() { "1", "2", "3", "4", "5" };

}

The most important difference is that a List can change in size. With an array, you can assign new values to each slot, but you can’t increase or decrease the number of slots (other than by creating a whole new array).

Your comment on line 8 of your example looks dubious, by the way–that will create an array with 3 slots, but all of the strings in it will be null. It won’t contain “3”.

2 Likes