1.2 program structure

Source: Internet
Author: User

C # key concepts of the program structure are program, namespace, type, member, and assembly. C # The program contains one or more source files. The declared type in the program. The type contains members and can be organized into the namespace. Class and interface are examples of types. Fields, methods, attributes, and events are member examples. When C # programs are compiled, they are physically packaged into the program set. The extended name of a program set is generally .exe or. dll, depending on whether it implements the application Appliction or the class library ).

See the following example:


Using System;

Namespace Acme. Collections

{

Publicclass Stack

{

Entry top;

Public void push (object data ){

Top = new Entry (top, data );

}

Public object Pop (){

If (top = null) throw newInvalidOperationException ();

Objiect result = top. data;

Top = top. next;

Return result;

}

Class Entry

{

Public Entry next;

Public Entry data;

Public Entry (Entry next, object data ){

This. next = next;

This. data = data;

}

}

}

}


In the namespace named "Acme. Collections", the class declared as a Stack is named "Acme. Collections. Stack. It includes several members: a field named top, two methods named push and pop, and a nested class named Entry. The Entry class further includes three members: a field named next, a field named data, and a constructor.

Assume that the source program in this example is saved as an acme. cs file, which indicates the behavior:

Csc/t: library acme. cs

Compile this example as a class library without the Main entry point code) and generate an Assembly named acme. dll.


The Assembly includes executable code in the form of Intermediate Language, IL, and symbolic information in the form of metadata. Prior to execution, the Assembly's IL code will be automatically converted to the code of a specific processor by the. NET Common Language Runtime, CLR.

Because an assembly is a self-describing functional unit that includes both code and metadata, # include commands and header files are not required in C. If a C # program needs to reference the public type and members of a specific assembly, it can simply reference the Assembly during compilation. For example, the following program uses acme. acme In the dll assembly. collections. stack class:


Using System;

Using Acme. Collections;


Class test

{

Static voidMain (){

Stack s = new Stack ();

S. push (1 );

S. push (10 );

S. push (1, 100 );

Console. WriteLine (s. pop ());

Console. WriteLine (s. pop ());

Console. WriteLine (s. pop ());

}

}


If the program is saved as a test. cs file, acme. dll can be referenced by the/r option when the test. cs file is compiled:

Csc/r: acme. dll test. cs

In this example, you can create an executable Assembly named test.exe.



C # allows the source files of a program to be stored as several source files. When a multi-file C # program is compiled, all the source files are processed together, and each source file can be freely referenced from each other in terms of concept, just like before processing, all source files are connected to a large file. In C #, the forward declaration is unnecessary because the Declaration Order is irrelevant. C # It does not limit that a source file can only declare one public type, and does not require that the source file name must match the type in the file.


This article from the "CSharp development topics" blog, please be sure to keep this source http://cssoffer.blog.51cto.com/7059827/1242389

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.