I am trying to make a generic method, that adds an object to the given array.
I tried the below code, but I get this error: "A property, indexer or dynamic member access may not be passed as an out or ref parameter"
public void Main()
{
Foo newObject = new Foo();
AddObjectToArray<Foo>(ref _allMyData.FooArray, newObject);
}
public void AddObjectToArray<T>(ref T[] array, T newObject)
{
var list = array.ToList();
list.Add(newObject);
array = list.ToArray();
}
I could solve it by removing the ref and returning the array like this:
_allMyData.FooArray = AddObjectToArray<Foo>(_allMyData.FooArray, newObject);
But it would be much cleaner if I could only use the ref :-)
Am I missing something obvious?