2009年12月16日 星期三

2d array overload C#

using System;

class Matrix
{
public const int DimSize = 3;
private double[,] m_matrix = new double[DimSize, DimSize];

// allow callers to initialize
public double this[int x, int y]
{
get { return m_matrix[x, y]; }
set { m_matrix[x, y] = value; }
}

// let user add matrices
public static Matrix operator +(Matrix mat1, Matrix mat2)
{
Matrix newMatrix = new Matrix();

for (int x=0; x < DimSize; x++)
for (int y=0; y < DimSize; y++)
newMatrix[x, y] = mat1[x, y] + mat2[x, y];

return newMatrix;
}
}

沒有留言:

張貼留言