• C#程序结构(学习笔记01)


    C#程序结构

    [原文参考官方教程]

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

    C#中的关键组织结构包括程序,命名空间,类型,成员和程序集

    以下示例在 Acme.Collections 命名空间中声明 Stack 类:

    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#编译打包的程序集包括可执行程序和库,扩展名为.exe和.dll

    编译成程序的语句,前提是包含main函数的程序,该实例为一个栈的实现程序,不包含main函数,只能编译为库文件
    命令行窗口执行:

    csc acme.cs
    

    编译成库文件
    命令行窗口执行:

    csc /t:library acme.cs
    

    创建一个使用acme.ll程序集的Acme.Collections.Stack类的程序:

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

    编译Example.cs程序文件并使用acme.dll程序集:
    命令行窗口执行:

    csc /r:acme.dll example.cs
    

    编译后生成Example.exe可执行文件,运行输出:

    100
    10
    1
    

    !!实际运行会因为执行过快,命令框会一闪而过

    使用 C#,可以将程序的源文本存储在多个源文件中。 编译多文件 C# 程序时,可以将所有源文件一起处理,并且源文件可以随意相互引用。从概念上讲,就像是所有源文件在处理前被集中到一个大文件中一样。 在 C# 中,永远都不需要使用前向声明,因为声明顺序无关紧要(除了极少数的例外情况)。 C# 并不限制源文件只能声明一种公共类型,也不要求源文件的文件名必须与其中声明的类型相匹配。

    前向声明是C++的一种文件引用方式,用于解决文件之间相互引用的问题

  • 相关阅读:
    BZOJ2142: 礼物
    GYM100633J. Ceizenpok’s formula 扩展lucas模板
    Codeforces451E Devu and Flowers
    BZOJ2982: combination Lucas模板
    UVAL3700
    BZOJ2821: 作诗(Poetize)
    BZOJ2724: [Violet 6]蒲公英
    Luogu P1858 多人背包
    Luogu P2831 愤怒的小鸟(状压+记忆化搜索)
    Luogu P4205 [NOI2005]智慧珠游戏
  • 原文地址:https://www.cnblogs.com/asahiLikka/p/11628316.html
Copyright © 2020-2023  润新知