(original) C # learning note 09--Defining class 05--Class Library project

Source: Internet
Author: User

9.5 Class Library Project

In addition to putting classes in different files in your project, you can put them in completely different projects . If a project contains nothing and contains only classes (and other related type definitions, but no entry points), the project is called a class library .

  when a class library project is compiled into a. dll assembly and a reference to a class library project is added to another project, its contents can be accessed (this can be part of the same solution, but this is not required). This expands the encapsulation provided by the object, because the class library can be modified and updated without affecting other items that use them. This means that you can easily upgrade the services provided by the class (which affects multiple user applications).

Here's an example of a class library project and a standalone project that leverages the classes that the project contains.

(1) Create a "class library" type of new project in the Chapter09 directory as shown in ch09classlib,9-10.

(2) Rename the file Class1.cs to MyExternalClass.cs "in the Solution Explorer (Solution Explorer) window, right-click the file and select Rename (Rename) to rename the file name." In the dialog box that pops up, click Yes.

(3) The code in MyExternalClass.cs automatically changes to reflect the change in class name:

 Public class

(4) Add a new class to the project using the file name MyInternalClass.cs.

(5) Modify the code to make the class Myinternalclass an inner class:

Internal class Myinternalclass {}

(6) Compile the project "Note that there is no entry point for this project, so you cannot run it as usual – you can choose" Generate? Build the solution "(build? Build solution) menu item to generate it ".

(7) Create a new console Application project ch09ex02 in the \chapter09 directory.

(8) Select "Project?" Add Reference "(Project? ADD Reference) menu item, or in the Solution Explorer (Solution Explorer) window, right-click References (References) and select the same option.

(9) Click the Browse button, navigate to \chapter09\chapter09\ch09classlib\bin\debug\, and double-click Ch09ClassLib.dll.

(10) After you have completed the above operation, check that the reference has been added to the Solution Explorer window, 9-11, as shown in Solution Explorer.

(12) Modify the code in the Program.cs as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;using Ch09classlib;namespacech09ex02{classProgram {Static voidMain (string[] args) { Myexternalclass myObj = new Myexternalclass ();            Console.WriteLine (Myobj.tostring ());        Console.readkey (); }    }}

(13) Run the application, as shown in result 9-13.

  

Description of the example

The Class Library project Ch09classlib contains two classes of Myexternalclass (publicly accessible) and Myinternalclass (accessible only internally). Note that by default, the class is implicitly determined for internal access because it does not have an access modifier. It is better to explicitly specify accessibility because it makes the code easier to understand, so the directive adds the internal keyword.

Note: When an application uses a class defined in an external class library, it can refer to the application as a client application for the library. The code that uses the defined class is generally referred to as the customer code.

In order to use the classes in Ch09classlib, a reference to Ch09ClassLib.dll is added to the console application. For this example, the reference is an output file to the class library, or it can be copied to the local location of CH09EX02 to continue developing the class library without affecting the console application. In order to replace the old version of the assembly with the new class library project, simply overwrite the old file with the newly generated DLL file .

Once you have added a reference, you can use the Object Browser to view the available classes. because the class myinternalclass is internal , this class is not visible in the Object Browser window- It cannot be accessed by an external project . However, Myexternalclass is accessible, which is the class we use in the console application.

  The technique of using classes from outside assemblies is key to programming with C # and. NET Framework. In fact, values are used by any class in the. NET framework, that is, in the use of classes in external assemblies, because they are handled the same way.

(original) C # learning note 09--Defining class 05--Class Library project

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.