C # program structure (study notes 01)

C # program structure

[Original refer to the official tutorial]

https://docs.microsoft.com/zh-cn/dotnet/csharp/tour-of-csharp/program-structure

 

Key organizational structure, including a program in C #, namespaces, types, members, and assemblies

The following example namespace declaration Stack class in Acme.Collections:

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;
            }
        }
    }
}

C # compiler package assembly including executables and libraries, extension .exe and .dll

Compiled into the program statement, provided that the program contains the main function, the instance is a stack implementation program, does not include the main function, can only be compiled as a library file
command line window to perform:

csc acme.cs

Compiled into a library file
command line window to perform:

csc /t:library acme.cs

Create a Acme.Collections.Stack class acme.ll assembly uses:

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());
    }
}

Compile Example.cs program file and use acme.dll Assembly:
command line window to perform:

csc /r:acme.dll example.cs

Example.exe compiled executable file, run the output:

100
10
1

! ! The actual running too fast because of the implementation of the command box will flash across

Using C #, a plurality of source files can be stored in the program in the source text. When you compile multi-file C # program that can be processed together all the source files, and source files can freely reference each other. Conceptually, like all source files are centralized into one large file before processing the same. In C #, you never need to use forward declarations, because the declaration order does not matter (except for a few exceptions). C # does not limit the source file type can only be a kind of public statement, nor does it require the source file name must match the type in which the declaration.

Forward declarations C ++ is a reference document to address issues of mutual references between files

Guess you like

Origin www.cnblogs.com/asahiLikka/p/11628316.html