Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap an int[,] with something like a ReadOnlyCollection?

Tags:

c#

.net

Seems like I can't do this:

private int[,] _table = new int[9, 9];
private ReadOnlyCollection<int[,]> _tableReadOnly = new ReadOnlyCollection<int[,]>(_table);

My idea is to have a property that let's the user read _table, but I don't want to let them change it, so I thought I could use ReadOnlyCollection for the matter.

like image 281
devoured elysium Avatar asked Dec 08 '25 08:12

devoured elysium


1 Answers

The ReadOnlyCollection is a one dimensional collection. You could make a ReadOnlyCollection<ReadOnlyCollection<int>>, or make your own two dimensional wrapper class, something like:

public class ReadOnlyMatrix<T> {

   private T[,] _data;

   public ReadOnlyMatrix(T[,] data) {
      _data = data;
   }

   public T this[int x, int y] {
      get {
         return _data[x, y];
      }
   }

}
like image 87
Guffa Avatar answered Dec 11 '25 07:12

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!