Thursday, 29 September 2016

how-to-add-value-to-my-matrix-type-like-an-array

static void Main(string[] args)
        {
            Matrix<int> m = new Matrix<int>(10);
            m[0, 0] = 10;
m[0, 1] = 20;
            Console.WriteLine(m[0,0]);
Console.WriteLine(m[0,1]);
            Console.ReadLine();
        }
// Define other methods and classes here
class Matrix<T> where T : IComparable, IComparable<T>, IConvertible, IEquatable<T>, IFormattable
    {
        #region typedef
        private List<List<T>> matrix;
        #endregion
        #region overloading
        public T this[int row, int col]
        {
            get { return matrix[row][col]; }
            set { matrix[row][col] = value; }//ArgumentOutOfRangeException
        }
        #endregion
        #region functions
        public Matrix(int size)
        {
            matrix = new List<List<T>>(size);
            for(int i=0;i<size;++i)
            matrix.Add(new List<T>(Enumerable.Repeat(default(T), size)));
        }
        #endregion
    }

No comments:

Post a Comment