C #, in-depth understanding (V)

Source: Internet
Author: User

3. Create a C # class library (Dll)
Creating a dll file in VC ++ in the past cannot be simple, but in Visual C #, this is also a breeze. The introduction below is divided into two parts: 1. Create DLL; 2. Test dll on the client.
(1) create a DLL
First, create an empty class library project. In. in the. NET integrated environment (IDE), select File> New> Project File> Visual C # project> class library and Click Browse) select the project file name and corresponding directory, and then click OK.
Next, let's take a look at the project and its related documents. Solution Explorer adds two C # classes to the project. The first is AssemblyInfo. cs, and the second is Class1.cs. We will not discuss AssemblyInfo, but will focus on Class1.cs.


Double-click Class1.cs to view a namespace mcMath. We will reference this namespace in the client to use this class Library:
Namespace mcMath
{
Using System;
/// <Summary>
/// Summary description for Class1.
/// </Summary>
Public class Class1
{
Public Class1 ()
{
//
// TODO: Add Constructor Logic here
//
}
}
}
Now you can Build (construct) this project. After the Build (construction) is completed, the mcMath. dll file is generated in the bin/debug directory of the project file.
Add a method
Open the ClassView (Class View) from the View menu, and only the Class1 is displayed, with no methods or attributes. Add a method and an attribute.
Right-click "Class1" and choose "Add (Add)"> "Add Method". The C # Method generation wizard is displayed:


In this window, add the method name, access type, return type, parameters, and comment information. You can use the Add and Remove buttons to Add and cancel parameters from the parameter list. Here we Add a method long Add (long val1, long val2), which is used to Add two numbers and return and.
Add an attribute
Similarly, you can use the C # property generation Wizard to add an attribute to the class:


After a method and an attribute are added, Class1 is shown as follows:


Observe the Class1 carefully and you will find that the C # Wizard program adds the following two functions to the class:
Public long Add (long val1, long val2)
{
Return 0;
}

Public bool Extra
{
Get
{
Return true;
}
Set
{
}
}
Add code to the class
Here we change Class1 to mcMathComp, because Class1 is a name that is easy to cause confusion. It may cause problems when we want to use this class in a customer application. The following code makes some adjustments to the above:
Namespace mcMath
{
Using System;
Public class mcMathComp
{
Private bool bTest = false;
Public mcMathComp ()
{
}
Public long Add (long val1, long val2)
{
Return val1 + val2;
}
Public bool Extra
{
Get
{
Return bTest;
}
Set
{
BTest = Extra;
}
}
}
}
Construct dll
Select the Build menu to create the dll file. If everything is OK, the dll file will be generated in the bindebug directory of the project file.
(2) Test the dll on the client
It is also very easy to Call dll methods and properties on the client. Please follow the steps below:
① Create a console application
In. in the. net ide integrated environment, select "file-> New-> project file-> Visual C # project file-> console application" to test the dll in this console application.
② Add a namespace reference
Select "Project-> Add reference", browse the file to find the dll, and click OK:




The reference adding wizard adds reference to the relevant library to the current project file:


③ Call the mcMath namespace, create the mcMathComp object, and call its methods and properties.
It is only one step away from calling component methods and attributes. Follow these steps:
● Reference namespace: using mcMath
● Create a mcMathComp object: mcMathComp cls = new mcMathComp ();
● Call methods and attributes
McMathComp cls = new mcMathComp ();
Long lRes = cls. Add (23, 40 );
Cls. Extra = false;
The complete project file code is as follows:
Namespace mcClient
{
Using System;
Using mcMath;
/// <Summary>
/// Summary description for Class1.
/// </Summary>
Public class Class1
{
Public Class1 ()
{
//
// TODO: Add Constructor Logic here
//
}
Public static int Main (string [] args)
{
McMathComp cls = new mcMathComp ();
Long lRes = cls. Add (23, 40 );
Cls. Extra = false;
Return 0;
}
}
}
Download the project file:McMath.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.