Program structure of C # in Visual C # Programming

Source: Internet
Author: User
The key concepts of program structure in C # Are program, namespace, type, member, and assembly. C # The program contains one or more source files. 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 assembly. The file extension name of the program set is generally .exe or. dll, depending on whether they are implemented as an application or a library ).

Example:

Using system;
Namespace acme. Collections
{
Public class Stack
{
Entry top;
Public void push (Object Data ){
Top = new entry (top, data );
}

Public object POP (){
If (Top = NULL) throw new invalidoperationexception ();
Object result = top. Data;
Top = top. Next;
Return result;
}
Class entry
{
Public entry next;
Public object data;

Public entry (Entry next, object data ){
This. Next = next;
This. Data = data;
}
}
}
}

In the namespace named "Acme. Collections", declare the class named "stack". The fully qualified name of this class is "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. Command 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) commands and symbolic information in the form of metadata (metadata. Prior to execution, the Assembly's il code will be automatically converted into the code of a specific processor by the. NET Common Language Runtime Library (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, simply reference the Assembly during compilation. For example, the following program uses the acme. Collections. Stack class from the acme. dll assembly:

Using system;
Using acme. collections;

Class Test
{
Static void main (){
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, the compile CME. dll can be referenced by the/R option when test. CS is compiled:

CSC/R: acme. dll test. CS

In this example, an executable Assembly named test.exe can be created. The running result is as follows:

100
10
1

C # allows the source text 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 in the annotation 1.

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.