C#类库开发示例及在项目中该类库的方法

首先在VS中新建类库项目:

接着,修改自动生成的类,或者将自动生成的类删掉然后添加新类:

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyMath
{
    public class MyMatrix
    {
        #region 字段
        private int[,] data;
        private int rowNum;
        private int colNum;
        #endregion

        #region 属性
        public int RowNum
        {
            get { return rowNum; }
            set { rowNum = value; }
        }
        public int ColNum
        {
            get { return colNum; }
            set { colNum = value; }
        }
        #endregion

        #region 构造函数
        public MyMatrix(int r, int c)
        {
            rowNum = r;
            colNum = c;
            data = new int[rowNum, colNum];
        }

        public MyMatrix(int[,] da)
        {
            this.data = da;
            rowNum = da.GetUpperBound(0)+1;//获取 System.Array 的指定维度的上限,此处是第一维
            colNum = da.GetUpperBound(1)+1;   //获取 System.Array 的指定维度的上限,此处是第二维
            
        }
        #endregion

        #region 索引器
        public int this[int r,int c]
        {
            get
            {
                if (r >= this.rowNum || c >= this.colNum)
                    throw new ApplicationException("行列序号超出范围");
                else
                    return data[r, c];
            }
            set
            {
                if (r >= this.rowNum || c >= this.colNum)
                    throw new ApplicationException("行列序号超出范围");
                else
                   data[r, c]=value;
            }
        }
        #endregion

        #region 重载+、-运算符
      
        public static MyMatrix operator +(MyMatrix m1, MyMatrix m2)
        {
            MyMatrix re = new MyMatrix(m1.RowNum, m1.ColNum);
            if (m1.colNum != m2.colNum || m1.rowNum != m2.rowNum)
                throw new ApplicationException("行列数不一致,无法进行矩阵加减");
            
            for (int i = 0; i < m1.rowNum; i++)
            {
                for (int j = 0; j < m1.colNum; j++)
                {
                    re[i, j] = m1[i, j] + m2[i, j];
                }
            }
            return re;
        }
        public static MyMatrix operator -(MyMatrix m1, MyMatrix m2)
        {
            if (m1.colNum != m2.colNum || m1.rowNum != m2.rowNum)
                throw new ApplicationException("行列数不一致,无法进行矩阵加减");
            MyMatrix result = new MyMatrix(m1.rowNum, m1.colNum);
            for (int i = 0; i < m1.rowNum; i++)
            {
                for (int j = 0; j < m1.colNum; j++)
                {
                    result[i, j] = m1[i, j] - m2[i, j];
                }
            }
            return result;
        }
        #endregion

        #region 方法


        public MyMatrix Transpose()
        {
            MyMatrix r = new MyMatrix( colNum,rowNum);
            for (int i = 0; i <rowNum ; i++)
            {
                for (int j = 0; j < colNum; j++)
                {
                    r.data[j,i] = data[i, j];
                }
            }
            return r;
        }
        public string ToMatrixString()
        {
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < rowNum; i++)
            {
                for (int j = 0; j < colNum; j++)
                {
                    s.Append(data[i, j].ToString() + "\t");
                }
                s.Append("\n");
            }
            return s.ToString();
        }
        #endregion
    }
}

为了使用这个类库,需要如下生成:


这样就可以生成一个可以供我们使用的dll引用了:


然后就可以在我们的项目中通过添加这个引用加以使用了:


添加引用后,还可以在我们的项目中通过对象浏览器查看其成员:


在我们的项目中添加如下代码就可以使用了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyMath; //因为MyMatrix定义在命名空间MyMath中

namespace TestClassLib
{
    class Program
    {
        static void Main(string[] args)
        {
            MyMatrix Array1 = new MyMatrix(6, 8);
            Random r = new Random();
            for (int i = 0; i < Array1.RowNum; i++)
            {
                for (int j = 0; j < Array1.ColNum; j++)
                {
                    Array1[i, j] = r.Next(100);//返回一个小于所指定的最大值的非负随机数
                    //Array1[i, j] = r.Next(0, 100);//随机数区间:[0,100)
                }
            }

            MyMatrix Array2 = new MyMatrix(6, 8);
            for (int i = 0; i < Array1.RowNum; i++)
            {
                for (int j = 0; j < Array1.ColNum; j++)
                {
                    Array2[i, j] = r.Next(100);//返回一个小于所指定的最大值的非负随机数
                    //Array1[i, j] = r.Next(0, 100);//随机数区间:[0,100)
                }
            }

            string s = Array1.ToMatrixString().ToString();
            Console.WriteLine("原来的矩阵Array1为:\n" + s);

             s = Array2.ToMatrixString().ToString();
            Console.WriteLine("原来的矩阵Array2为:\n" + s);

            MyMatrix ArraySum = Array1 + Array2;//调用重载+方法

            s = ArraySum.ToMatrixString().ToString();
            Console.WriteLine("Array1 + Array2为:\n" + s);

            MyMatrix ArraySubtract = Array1 - Array2;//调用重载-方法

            s = ArraySubtract.ToMatrixString().ToString();
            Console.WriteLine("Array1 - Array2为:\n" + s);

            MyMatrix ArrayTranspose = Array1.Transpose();

            s = ArrayTranspose.ToMatrixString().ToString();
            Console.WriteLine("转置后的矩阵为:\n" + s);

            Console.ReadKey();
        }
    }
}


效果如下:


完!


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值