Creating a New List in Abstract Override Using Get/Set?

This might be more of a C# than a Unity question and I can’t help but think the answer is staring me in the face buuuuttt…

Let’s say I have a Class A:

public abstract class ClassA : MonoBehaviour
{

public abstract List<Int> exampleList { get; set; }

}

And a class B:

 public class ClassB : MAI_Module
{

public override List<Int> exampleList
        {
            get
            {
               
                return exampleList;
            }

            set
            {
                exampleList = new List<Int>();
                exampleList.Add(1);
                exampleList.Add(10);
                exampleList.Add(100);
                exampleList = value;
            }
        }

}

How can I create a new list for class B? (Please note the get/set in ClassB was just me trying things)
What I’m trying to do is this:

//This code is in a class C:

classes = new List<ClassA>();

//Get all the ClassA's on the gameObject:

foreach (ClassA a in gameObject.GetComponents<ClassA>())
            {
                classes.Add(a);
            }

//Create a new list of all the Ints from all the ClassA exampleLists:

            exampleList = new List<Int>();

            foreach (ClassA a in classes)
            {
                foreach (Int number in a.exampleList)
                {
                    exampleList.Add(number);
                }
            }

I just can’t seem to figure out how to create a new list in ClassB but still be able to get it from all the ClassA’s in ClassC (since ClassB is derived from ClassA).

Any help is appreciated, I can clarify my question if needed. Thanks :slight_smile:

A parent can’t access child properties, can you use concrete example than class A B C ?

I don’t understand what you want to do. ClassA doesn’t have a List, it just has an abstract declaration that any implementations will have a list.

ClassB’s exampleList.get is infinitely recursive:

public override List<Int> exampleList
{
    get
    {
        return exampleList;
    }
...

exampleList just calls the get again. If you’ve got a list like that, you’ll need to return some different list:

private List<int> _exampleList;
public override List<Int> exampleList { get { return _exampleList; } }

This can be used to make exampleList be lazy (only exist when you need it):

private List<int> _exampleList;
public override List<Int> exampleList { 
    get { 
        if(_exampleList == null) {
            _exampleList = new List<int>();
        }
        return _exampleList; 
    } 
}

Or if you like compact code:

private List<int> _exampleList;
public override List<Int> exampleList { 
    get { 
        return _exampleList ?? (_exampleList = new List<int>());
    } 
}

Yes, you’re right. For some reason I was trying to treat the override list like ClassA’s without ClassB having it’s own list to set it’s parent to, I’m not sure how I missed that.

This worked for me:

private List<Int> _exampleList;
        public override List<Int> exampleList
        {
            get
            {
                if(_exampleList == null)
                {
                    _exampleList = new List<Int>();
                    _exampleList.Add(10));
                }
                return _exampleList;
            }
        }

Thanks.