Arrays with classes using { get; set; }

So I’ve been searching for a way to use arrays in classes, but I honestly don’t know what to look for and I can’t find an example of anyone doing this online.

Can anyone tell me what the correct format?

public class ClassExample
{
            public int[] example1 { get; set; } // Doesn't give an error, but you can't set it with example1 = 5
            public int[] example2 = new int[5] { get; set; } //Causses get and set to give an error
}

I think you’re muddling up fields and properties.

A field is a member of a class that is actually stored in memory:

public class ClassExample
{
    public int[] example1;
}

You don’t need to write getters or setters; you can just access it:

void Start()
{
    example1 = new int[5]; // allocate an array of 5 integers
}

void Update()
{
    example1[0] += 2;   // add two to the first integer in the array
}

A property on the other hand is not stored in memory. It simply exposes a way to access a value (read via get, or write via set, or both). That value might, underneath, be a field, or it might not:

public class ClassExample
{
    private int[] example1;

    public int FirstValue { get => example1[0]; }
}

EDIT FOR CORRECTNESS - removed misleading info about properties.

2 Likes

To elaborate on what was said above, this:

public int[] example1 { get; set; }

Is the syntax for an auto-property. This is just a shorthand for this:

private int[] _example1;
public int[] example1
{
    get
    {
        return _example1;
    }
    set
    {
        _example1 = value;
    }
}

Your second line

public int[] example2 = new int[5] { get; set; }

Just makes no sense. You declared an int array field (which is the first part public int[ ] example2). You also used a field initializer (because you have an equals sign right after the field declaration). Behind the “=” is the actual value the field is initialized to. This creates a new int array with 5 elements new int[5] . An array constructor can have an array / list initializer behind it that would contain the actual values you want to put into the array. Like this new int[ ] {1, 42, 5, 123 }.

Using the keywords get / set inside an array initializer makes no sense.

1 Like

All of the above is valid, but I figure I’d add that auto-properties can be initialized - you just need to place the initialization code after the { get; set; } part:

public int[] example2 { get; set; } = new int[5];

// OR

public int[] example2 { get; set; } = new int[] { 1, 2, 3, 4, 5 };
2 Likes

Every day’s a school day. Will amend my post to avoid confusion if people only read that far.

Maybe back up a little? You wrote that you want to use arrays with classes. That would be something like an array of Cats (where Cat is a class with name, age and cuteness). But your example isn’t using a class – just an array of ints, which isn’t a class.

So maybe instead ask something like “I want an array of numbers – how to write that?” or “I want to practice with get/get, what’s the simplest example of using those and also having an array?”

Okay, so I’m a little confused. Are you saying that if I want to create a class to use as a database of objects with certain properties in my game, I don’t have to use {get; set;} at all? Sorry.

Oh, and could you maybe tell me how to add data to it? list.Add(new Faction() {example[5] = 5}) doesn’t seem to work.

list.Add(new Faction() { example = new int[] { 1, 2, 3, 4, 5 } });

You can do it that way. If you need something more elaborated you will need to make a constructor or a function that will return an int[ ];

Yes – get and set are completely optional. A fully functional class might look like:

public class Item {
  public string name, description;
  public int cost;
}

If you have public Item[ ] ItemList; then Unity will add a list of those in the Inspector. It starts with 0 items, but you can type a larger value, then type into the name/desc/cost slots.

Okay, thank you guys