One of the series "open the door to C #", learn to release multiple projects

Source: Internet
Author: User

My cousin wants to learn programming. I recommend him to learn. NET and C #. This recommendation is not tight, but I assume the guiding role. I am on a business trip, but direct tutoring is not good. It is too troublesome to send emails. I have recommended a few books. Unfortunately, he is too fond of food. Do I feel this way when I start C? So we have this series of articles. My cousin brought him into the computer industry. I didn't know anything at the beginning. I used to open the computer chassis and instruct him where is the hard disk, where is the memory, and where is the CPU, now he can be a master of computer hardware. I hope you can also learn software programming.

1. solution, project, assembly, and namespace

Beginners can easily confuse these concepts. Let's talk about projects first. In general, a project can be a software you develop. In. Net, a project can be displayed in various types, such as console applications, Windows applications, class libraries, web applications, Web Services, and Windows controls. After compilation and translation, the application will be compiled into an. EXE file, and the rest will be compiled into a. dll file. Both the. exe file indicates that it can be executed, as shown in the program. These applications all have a main program entry point, that is, the method main (). Class Libraries and Windows controls do not have this entry point, so they cannot be directly executed. Instead, they only provide some functions for other projects to call.

In Visual Studio. NET, you can select "new" and "project" from the "file" menu to create a new project. For example, create a console application. Note that in addition to creating a console project, Visual Studio also provides a solution ). What is the purpose of this solution? If you only need to develop a hello World project, the solution will naturally be useless. However, a slightly more complex software requires many modules. To reflect the hierarchical relationship between each other and facilitate program reuse, multiple projects are often required, each project implements different functions. Finally, these project groups are combined to form a complete solution. In an image, the solution is a container, which is divided into multiple layers and multiple cells to store different projects. The relationship between a solution and a project is greater than or equal. After the solution is created, a file with the. sln extension will be created.

Add a project to the solution. You cannot use the "new" method. Instead, select "add Project" in the "file" menu ". The added project can be a new project or an existing project.

Assembly is called assembly. I don't want to mention the academic concept. In general, a project is also an assembly. From the design perspective, it can also be regarded as a complete module or package ). Therefore, an assembly can also be reflected as a DLL file or EXE file. There are many articles about how to divide an assembly, but beginners do not need to consider it for the moment.

Namespace is a concept in C ++. It is introduced to avoid conflicts of the same object names in a project. This namespace has no special requirements. But basically, to ensure its uniqueness, it is best to use the URI format, such as brucezhang.com. This namespace is a bit like the last name in our name, and then the name of each object is the name in the name. If there are duplicates, you can add the middle name to foreign names. The names are brave. The names are separated by different surnames, either Zhang Yong or Zhao Yong. Of course, most people have repeated surnames, so we try to be as complicated as possible when naming a namespace.

Many beginners often regard a project as a namespace. In fact, there is no absolute relationship between the two. In the project, we can also define many different namespaces. However, for ease of use, it is recommended that the namespace of a project be an integrated hierarchy. In Visual Studio, we can create a new folder in the project. By default, the namespace of the objects in the folder should be "project namespace. Folder name ". Of course, we can also modify it in namespace.

You can set the namespace and set name in Visual Studio. Right-click the project name to bring up the following dialog box:

 




In the figure, assembly name is the name of the Assembly. If the Assembly name is compiled, it is the file name of the project. The default namespace is the default namespace. When developing software, we should develop good habits. After creating a new project, we should set these attributes. Once the default namespace is set, the namespace of the newly created object is the set value. As for the set name, if it is a DLL file, it is recommended that its name be the same as the default namespace.

Instance drill:

(1) Create the console application "Hello world !"

1. Open Visual Studio. NET, select "new" from the "file" menu, and select "project ";

2. Select "console application" in Visual C # projects ",:

 



In location, locate the path of the project you want to save, and the name is "firstexample ". This name is both the solution name and the project name.

3. Right-click the project name. In the displayed dialog box, name the Assembly name helloworld and the default namespace brucezhang.com. firstexample.

4. At this time, a file named class1.cs has been created in Visual Studio (if it is Visual Studio 2005, it is program by default. CS); Modify the file name to helloworld. CS. Modify the namespace and class name in the file as follows:

Namespace brucezhang.com. firstexample

{

/// <Summary>

/// Summary description for class1.

/// </Summary>

Class helloworld

{

/// <Summary>

/// The main entry point for the application.

/// </Summary>

[Stathread]

Static void main (string [] ARGs)

{

//

// Todo: Add code to start application here

//

}

}

}

5. Note that there is a main () method in helloworld. CS. This is because we have created a console application. Add the following code to the main () method:

Console. writeline ("Hello world! ");

Console. Read ();

The console here is a class that can operate on the console.

6. Run.

Check the saved project path folder firstexample/bin/debug, and a helloworld.exe file exists.

(2) Add a new project for the solution

1. In the "file" menu, select "add project" and add "new project ". In the dialog box, select "class library" and name it printer. As for the SAVE path, you can put it in the firstexample folder created earlier:



2. on the right side of Visual Studio, we can see that there are now two projects. The new project name and default namespace name are still modified, both of which are brucezhang.com. printer.

3. Change the default class1.cs name to messageprinter. CS and modify its code:

Namespace brucezhang.com. Printer

{

/// <Summary>

/// Summary description for class1.

/// </Summary>

Public class messageprinter

{

Public messageprinter ()

{

//

// Todo: Add constructor logic here

//

}

Public static void print (string MSG)

{

Console. writeline (MSG );

}

}

}

In the messageprinter class, we noticed that there is no main () method because it is not an application. The new print () method can receive a string and display it in the console.

4. Compile the printer project. Right-click the project name and select "build" from the menu ". After compilation, find the printer/bin/debug folder and find the brucezhang.com. printer. dll file, which is the final assembly file.

5. associate these two projects. We want to use the print () method of the printer project in the firstexample project, provided that a reference to the printer project needs to be added to the firstexample project. Right-click "Reference" of the firstexample project, select "add reference", select the "project" tab in the dialog box, locate the project, and select it. Finally:



 

6. Now you can use messageprinter in the firstexample project. First, add using to the namespace, and then call it in the main () method. The final code is as follows:

Using system;

Using brucezhang.com. printer;

Namespace brucezhang.com. firstexample

{

/// <Summary>

/// Summary description for class1.

/// </Summary>

Class helloworld

{

/// <Summary>

/// The main entry point for the application.

/// </Summary>

[Stathread]

Static void main (string [] ARGs)

{

Messageprinter. Print ("Hello world! ");

Console. Read ();

}

}

}

7. Run. The result is the same as that in the previous example.

In this example, the solution contains two projects: a console application and a class library. The Class Library provides some basic functions, such as the print () method in the example. We often put some common methods into the class library. In this way, other applications can call it. For example, the console application in this example. If the new windows application requires this function, you can directly reference the print () method of messageprinter without repeated implementation.

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.