I'm trying to create an immutable class to represent a matrix. However, while the private setters in the Rows and Columns is working I am still able to modify the contents of Elements. How do I properly create an immutable class?
var matrix = new Matrix(new double[,] {
{1,1,1,1},
{1,2,3,4},
{4,3,2,1},
{10,4,12, 6}});
matrix.Elements[1,1] = 30; // This still works!
Matrix class:
class Matrix
{
public uint Rows { get; private set; }
public uint Columns { get; private set; }
public double[,] Elements { get; private set; }
public Matrix(double[,] elements)
{
this.Elements = elements;
this.Columns = (uint)elements.GetLength(1);
this.Rows = (uint)elements.GetLength(0);
}
}