Several important terms in C # Series

Source: Internet
Author: User
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. SlaveDesignIt 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.