C# - 程序结构

在学习 C# 编程语言的基本构建块之前,让我们先了解一下 C# 程序的最小结构,以便在接下来的章节中作为参考。

C# 程序的基本结构

C# 程序遵循简单的结构,包含必要的组件。基本程序如下所示:

  • 每个程序都以 using 语句开头,这些语句用于包含必要的命名空间。
  • namespace(命名空间) 将相关的类组合在一起。
  • class(类) 包含程序逻辑。
  • Main() 方法是程序的入口点,程序从这里开始执行。
  • Main() 中的语句按顺序执行。

创建 Hello World 程序

让我们看一段打印"Hello World"的简单代码 -

using System;

namespace HelloWorldApplication {
   class HelloWorld {
      static void Main(string[] args) {
         /* 我的第一个 C# 程序 */
         Console.WriteLine("Hello World");
         Console.ReadKey();
      }
   }
}

此代码编译并执行后,将产生以下结果 -

Hello World

让我们来看看给定程序的各个部分 -

  • 程序的第一行 using System; - using 关键字用于在程序中包含 System 命名空间。一个程序通常包含多个 using 语句。

  • 下一行是 namespace 声明。namespace 是类的集合。HelloWorldApplication 命名空间包含 HelloWorld 类。

  • 下一行是 class 声明,HelloWorld 类包含程序使用的数据和方法定义。类通常包含多个方法。方法定义了类的行为。但是,HelloWorld 类只有一个方法 Main

  • 下一行定义了 Main 方法,它是所有 C# 程序的入口点Main 方法声明了类在执行时执行的操作。

  • 下一行 /*...*/ 会被编译器忽略,它会在程序中添加注释

  • Main 方法使用语句 Console.WriteLine("Hello World");

    WriteLineConsole 类的一个方法,该类定义在 System 命名空间中。此语句会显示消息"Hello, World!"显示在屏幕上。

  • 最后一行 Console.ReadKey(); 适用于 VS.NET 用户。这会使程序等待按键,并防止从 Visual Studio .NET 启动程序时屏幕快速运行和关闭。

C# 程序的关键组件

1. 命名空间声明

命名空间通过将相关类分组在一起来帮助组织大型程序。

namespace MyApplication
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello from MyApplication!");
        }
    }
}

当编译并执行上述代码时,它会产生以下结果 -

Hello from MyApplication!

2. 使用指令 (using)

using 语句导入命名空间,允许访问内置类和方法。

using System; // 启用 Console.WriteLine() 函数

class Example
{
    static void Main()
    {
        Console.WriteLine("Using directive example.");
    }
}
    

当编译并执行上述代码时,它会产生以下结果 -

Using directive example.

3. 类声明

C# 是一种面向对象的语言,每个程序都必须至少有一个类。

class Car
{
    string model = "Tesla";

    static void Main()
    {
        Car myCar = new Car();
        Console.WriteLine("Car Model: " + myCar.model);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

Car Model: Tesla

4. Main() 方法

Main() 方法是每个 C# 程序的入口点。

using System;

class Start
{
    static void Main()
    {
        Console.WriteLine("This is the main entry point of the program.");
    }
}

当编译并执行上述代码时,它会产生以下结果 -

This is the main entry point of the program.

5. 语句和表达式

Main() 方法中的每条指令都是一条语句。

class StatementsExample
{
    static void Main()
    {
        int a = 5, b = 10;
        int sum = a + b;
        Console.WriteLine("Sum: " + sum);
    }
}

当编译并执行上述代码时,它会产生以下结果 -

Sum: 15

6. 访问修饰符

访问修饰符定义类和方法的可见性。

类示例
{
	private int secretNumber = 42; // Private:仅在此类内可访问

    public void Display()
    {
        Console.WriteLine("Access Modifier Example: " + secretNumber);
    }
}

class Program
{
    static void Main()
    {
        Example obj = new Example();
        obj.Display();
    }
}

当编译并执行上述代码时,它会产生以下结果 -

Access Modifier Example: 42

组织 C# 程序

结构良好的程序更易于阅读、运行更流畅、维护更简单。

  • 您应该使用适当的缩进来提高代码的可读性。
  • 定义标识符时,请为类和方法指定有意义的名称,以明确其用途。
  • 方法应简短且专注于单一任务。
  • 必须遵循命名规则(类使用 PascalCase 命名法,变量使用 CamelCase 命名法)。
  • 将相关的类存储在单独的文件中,以保持代码的条理性。

示例:结构良好的 C# 程序

以下是一个真实示例,演示了一个包含多个组件的结构化 C# 程序:

using System;

namespace School
{
    class Student
    {
        public string Name;
        public int Age;

        public void DisplayStudentInfo()
        {
            Console.WriteLine("Student Name: " + Name);
            Console.WriteLine("Student Age: " + Age);
        }
    }

    class Program
    {
        static void Main()
        {
            Student student1 = new Student();
            student1.Name = "Alice";
            student1.Age = 14;

            student1.DisplayStudentInfo();
        }
    }
}

当编译并执行上述代码时,它会产生以下结果 -

Student Name: Alice
Student Age: 14

需要注意以下几点 -

  • C# 区分大小写。

  • 所有语句和表达式必须以分号 (;) 结尾。

  • 程序从 Main 方法开始执行。

  • 与 Java 不同,程序文件名可能与类名不同。

编译和执行程序

如果您使用 Visual Studio.Net 编译和执行 C# 程序,请执行以下步骤 -

  • 启动 Visual Studio。

  • 在菜单栏上,选择"文件"->"新建"->"项目。

  • 从模板中选择"Visual C#",然后选择"Windows"。

  • 选择"控制台应用程序"。

  • 为您的项目指定名称,然后点击"确定"按钮。

  • 这将在解决方案资源管理器中创建一个新项目。

  • 在代码编辑器中编写代码。

  • 点击"运行"按钮或按 F5 键执行项目。出现一个包含"Hello World"行的命令提示符窗口。

使用命令行编译和执行程序

您可以使用命令行而不是 Visual Studio IDE 来编译 C# 程序 -

  • 打开文本编辑器并添加上述代码。

  • 将文件另存为 helloworld.cs

  • 打开命令提示符工具并转到保存文件的目录。

  • 输入 csc helloworld.cs 并按 Enter 键编译代码。

  • 如果代码中没有错误,命令提示符将带您到下一行并生成 helloworld.exe 可执行文件。

  • 输入helloworld 执行你的程序。

  • 你可以在屏幕上看到输出 Hello World。