分享

Visual C# .NET: How to Create a Class Library

 ShangShujie 2010-05-31

You can create a class library of your code, save it as a DLL and then reuse your C# code in other projects. The process is quite straightforward.

Start a new project. From the New Project dialogue box, select Class Library instead of Windows Application. In the Name box, type a name for your Class Library. Call it calculatorMethods, for this lesson. Then click OK.

When you click OK, C# will create a Namespace with the Name you've just used, and you'll be looking at the code window:

A new namespace

Note that the class is called Class1. You can change that to anything you like. But change it to calculator. Locate Class1.css in the Solution Explorer on the right:

The solution explorer showing a new class

Right click, and select Rename from the menu. Rename it to calculator.cs:

Rename your c# class

Press the Enter key on your keyboard, and you will then get a message box telling you that you are about to rename a file, and would you like to rename all references. Say Yes, and your code window will then look like this:

The class has been renamed

Class1 has now been renamed to something useful - calculator. You can now go ahead and add the code for your Class:

Two simple methods added to the c# class

In the image above, we've added two simple methods, one that adds up and one that subtracts. Note that they are both public static methods. This is only because static methods offer an easy way to set up a code library, and not down to any sort of recommended coding technique for class libraries. They are for illustration purposes only!

Now that you have some code, click Build > Build Solution from the C# menu bars at the top. Click File > Save As, as well. Now have a look at the Solution Explorer again. Click the Icon for All Files, and then expand the bin > Debug folder:

The bin > debug folder

And there's our DLL file! We can use this in other projects. To test it out, click File > Close Solution. Now start a new Windows Application project.

Add a new button to your form, and double click to get at the code. From the menu bars at the top of C#, click Project > Add Reference. You should see a dialogue box appear with a few tabs on it. Click the Browse tab. Browse to where you saved your Class Library. Then locate the Bin > Debug folder from above. Your DLL will be in there:

Add a reference to your dll file

Click OK to add the reference to your DLL. Have a look at the Solution Explorer on the right, and you should see it appear on the list of references:

A reference has been added to the dll file

Now that you have a reference, you can use your code.

Add the following to your button code:

The new namespace showing on the c# intellisense list

You should see your Namespace on the IntelliSense list. Press the Enter key on your keyboard to add it to your code. Then type a dot. Your class name should appear:

The class appears on the list

Again, press the Enter key on your keyboard to add it to your code. Type another dot and you'll see the methods:

The methods from the c# class

Finish the code, and you may have something like this:

C# code to use a dll file

Run your programme and it should work fine.

And there you have it - an easy way to create your own class libraries and reuse your C# code.

 

 

 

 

 

But that's it for this beginners course on Visual C# .NET. We hope you enjoyed it, and wish you all the best for your future programming career. Good luck!

 

<-- Drawing Text

<--Back to the C# .NET Contents Page

View all our Home Study Computer Courses

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多