I have a variable defined in one class, let's say class A:
private char[,] matrix;
Later in my code, this matrix is initialized and populated according to some conditions.
Now from another different class, let's say class B, I need to get the final matrix.
So I have two options:
Implement a public property in class A that will be accessed from class B:
public char[,] GetMatrix { get { if (this.matrix == null || this.matrix.Length == 0) { // Matrix is null or it has no elements on it. return null; } else { return this.matrix; } } }
Implement a public method in class A that will be accessed from class B:
public char[,] GetMatrix() { if (this.matrix == null || this.matrix.Length == 0) { // Matrix is null or it has no elements on it. return null; } else { return this.matrix; } }
As stated here it is recommended to use a method in case the operation returns an array.
Also it is said:
Use a method where the operation returns an array because to preserve the internal array, you would have to return a deep copy of the array, not a reference to the array used by the property. This fact, combined with the fact that developers use properties as though they were fields, can lead to very inefficient code
So in my case, which option is the best? and why?