C # Guide, revisit the basics and look far away! (2) Program structure

Source: Internet
Author: User
Tags dotnet

The key organizational structure concepts in C # include programs , namespaces , types , members , and assemblies. A C # program consists of one or more source files. The program declares the type, and the type contains the members and is organized into the namespace. examples of types include classes and interfaces. examples of members include fields, methods, properties, and events. a compiled C # program is actually packaged into an assembly. The assembly's file name extension is typically .exe or .dll , depending on whether the application or library is being implemented.

The following example Acme.Collections declares a class in the namespace Stack :

C # replication
Using System;Namespaceacme.collections{PublicClassStack {Entry top;PublicvoidPush (Object data) {top =New Entry (top, data); }Publicobject pop (if (top = null) {throw new InvalidOperationException (); } object result = top.data; top = Top.next; return result; class entry {public Entry next; Span class= "Hljs-keyword" >public object data; public Entry ( entry Next, object data) {this.data = data;} } }} 

The fully qualified name of this class is Acme.Collections.Stack . This class contains multiple members: A top field, two methods ( Push and Pop ), and a Entry nested class.  Entry The class also contains three members: a next field, a data field, and a constructor. Assuming that the source code for the sample is stored in a acme.cs file, the following command line

Copy
csc /t:library acme.cs

Compile the sample into a library ( Main code without entry points) and build the assembly acme.dll .

Important

The preceding example uses the csc C # compiler as the command line. This compiler is a Windows executable file. to use C # on other platforms, you should use. NET Core tools. . NET Core Ecosystem Use dotnet CLI to manage command-line builds. This includes managing dependencies and invoking the C # compiler. for a complete description of using these tools on a platform supported by. NET Core, see this tutorial.

The assembly contains the executable code in the form of intermediate language (IL) directives and symbolic information in the form of meta data. before execution, the IL code in the assembly is automatically converted to the processor-specific code by the. NET common language Runtime's just-in-time (JIT) compiler.

Because an assembly is a self-describing unit of functionality that contains code and metadata, you do not need to use #include directives and header files in C #. You can use the public types and members contained in this Assembly in a C # program by simply referencing a specific assembly when compiling the program. For example, this program uses acme.dll Acme.Collections.Stack classes in an assembly:

C # replication
using System;using Acme.Collections;class Example{    static void Main() { Stack s = new Stack(); s.Push(1); s.Push(10); s.Push(100); Console.WriteLine(s.Pop()); Console.WriteLine(s.Pop()); Console.WriteLine(s.Pop()); }}

If the program is stored in a file example.cs , example.cs You can use the compiler's/r option to reference the Acme.dll assembly after compiling it:

Copy
csc /r:acme.dll example.cs

This creates the example.exe executable assembly, which outputs the following at run time:

Copy
100101

With C #, you can store the program's source text in multiple source files. when compiling a multi-file C # program, you can process all the source files together, and the source files can be arbitrarily referenced to each other. Conceptually, it is like all source files are concentrated into a large file before processing. in C #, it is never necessary to use forward declarations, because the order of declarations is insignificant (except for very few exceptions). C # does not restrict source files from declaring only one public type, nor does it require that the source file's file name match the type it declares.

C # Guide, revisit the basics and look far away! (2) Program structure

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.