There's only one way to simulate this.
You need to create your own class, with a private array.
The most similar implementation of an array is an indexer:
The '10.8' link shows the simulation of a bidimensional array.
If you implement the indexer only with a getter, the user can only read the elements, but not write them. However, if each element is an object (reference type) you can't prevent the modification of the accessed objects properties.
However, there are several ways of simulating "read-only" objects:
- Create a wrapper class that exposes the properties of each element in the array as read only properties, so that they cannot be modified
- Using primitive value types (like
int
)
- Defeating the changes by returning a copy of the element in the private array instead of the original element in the private array, so that, the changes made to the object don't affect the original object in the array.
In other languages like C++ there are references and pointers to constant values, but this doesn't exist in C#.