1

I have this class property

public object[] array { get; set; }

I'm able to get and set the entire array, as well as alter the individual items within the array.

How can I also achieve this with manual get-setters?

None of the answers to these 3 posts:

How do define get and set for an array data member?

Array property syntax in C#

Get/Set Method for Array Properties

cover what I'm needing to do.

object[] array;
public object[] Array {
    get { return array; }
    set { array = value; }
}

would allow me to get and overwrite the entire array, but I wish to have indexal access.

1

1 Answer 1

1
public sealed class ArrayWrapper<T> {
   private readonly T[] _array;
   public ArrayWrapper(T[] array) {
      if (array == null) throw new ArgumentNullException(nameof(array));
      _array = array;
   }

   public T this[int i] {
      get { return _array[i]; }
      set { _array[i] = value; }
   }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Do you think this is simpler? stackoverflow.com/a/424677/7759514
It depends on what you're trying to do. What are you trying to do?
Call a function when values are set/get. I think I'll try the get/setValue functions first
Um, you can run whatever you want inside the set/get code I put above. How is that not brain-dead simple?
What I was getting at was it's possible over-complication. It's only about 4 lines, though.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.