码迷,mamicode.com
首页 > 其他好文 > 详细

C#自定义类库DLL

时间:2014-09-17 21:37:32      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   使用   ar   文件   2014   

定义顺序是:

新建一个项目

bubuko.com,布布扣

 

在解决方案上右键添加,新建项目:

bubuko.com,布布扣

 

在弹出的窗口中选择类库:

bubuko.com,布布扣

 

这样就新建了一个类库,在此项目中编写代码

如:

bubuko.com,布布扣

 

此时可以在另外的项目的主函数中调用:

bubuko.com,布布扣

 

此时尚且不能使用,需要在项目中的引用文件夹下添加引用:

bubuko.com,布布扣

在弹出的窗口中:

bubuko.com,布布扣

选择解决方法勾选刚刚建立的类库

 

此时再在主函数头添上一句using ClassLibrary1;就好了

bubuko.com,布布扣

 

之后,将主函数所在的项目设好为启动项目,就可以调试执行了。

 

使用命名空间的好处:

避免命名冲突;

简化程序,

例如,使用两个类库,分别属于不同的命名空间子分支:

类库1:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LHYZ.SuperLib
 8 {
 9     public class SquareWidget
10     {
11         public double SideLength = 0;
12 
13         public double Area
14         {
15             get { return SideLength * SideLength; }
16         }
17     }
18 }

类库2:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LHYZ.UrtraLib
 8 {
 9     public class CircleWidget
10     {
11         const double PI = 3.1415926;
12 
13         public double Radius = 0;
14 
15         public double Area
16         {
17             get { return PI * Radius * Radius; }
18         }
19     }
20 }

 

如果不在主函数源码文件中声明命名空间:

就必须这样使用:

bubuko.com,布布扣

 

使用命名空间后使用方法如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using LHYZ.SuperLib;
 7 
 8 namespace Test
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             SquareWidget sw = new SquareWidget();
15             sw.SideLength = 3;
16             Console.WriteLine(sw.Area);
17 
18             LHYZ.UrtraLib.CircleWidget cw = new LHYZ.UrtraLib.CircleWidget();
19             cw.Radius = 3.894;
20             Console.WriteLine(cw.Area);
21             Console.ReadKey();
22         }
23     }
24 }

 

C#自定义类库DLL

标签:style   blog   http   color   io   使用   ar   文件   2014   

原文地址:http://www.cnblogs.com/lhyz/p/3977961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!