C#类库开发示例及在项目中该类库的方法
2014-11-23 14:35
585 查看
首先在VS中新建类库项目:
接着,修改自动生成的类,或者将自动生成的类删掉然后添加新类:
代码如下:
为了使用这个类库,需要如下生成:
这样就可以生成一个可以供我们使用的dll引用了:
然后就可以在我们的项目中通过添加这个引用加以使用了:
添加引用后,还可以在我们的项目中通过对象浏览器查看其成员:
在我们的项目中添加如下代码就可以使用了:
效果如下:
完!
接着,修改自动生成的类,或者将自动生成的类删掉然后添加新类:
代码如下:
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(); } } }
效果如下:
完!
相关文章推荐
- PHP调用C#开发的dll类库方法
- PHP调用C#开发的dll类库方法
- C#编写的发送手机短信的类库 && C#开发短信的方法和简介 &&短信编程实例
- C#开发的类库dll,注册为Arcgis的插件tlb的方法
- PHP调用C#开发的dll类库方法
- VS2012打开V2013开发的项目C#方法
- C#编写的发送手机短信的类库 && C#开发短信的方法和简介 &&短信编程实例
- MapPoint+SmartPhone+C#开发示例
- MapPoint+SmartPhone+C#开发示例
- C# Web项目开发第一阶段沉思录
- [开发语言]C#项目如何制作安装程序
- 没头没尾--项目开发笔记 C#企业级模板理解
- C#开发终端式短信的原理和方法
- C#开发终端式短信的原理和方法
- C#开发终端式短信的原理和方法
- Myeclipse平台struts+hibernate+spring项目开发示例
- C#开发终端式短信的原理和方法
- C#开发终端式短信的原理和方法
- C#开发终端式短信的原理和方法(转自 蚕蛹)
- C#开发终端式短信的原理和方法(转自 蚕蛹)