一、类与命名空间

1、关系

namespace类似于一家大型医院,类就是医院下面的各科室,用于存放数据成员、方法成员、其他类等内容,便于调用。

2、使用指定命名空间中的类

  1. namespace HelloWorld
  2. {
  3. using Demo;
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Console.WriteLine("人因梦想而伟大");
  9. Demo.Operation oper = new();
  10. }
  11. }
  12. }
  13. namespace Demo
  14. {
  15. class Operation
  16. {
  17. }
  18. }

二、程序的启动器——Main方法

1、基本要求

  1. Main方法必须定义为static
  2. Main的首字母必须大写
  3. 返回值可以是void/int
  4. 命令行参数可选

    2、语法

    static void Main(string[] args) {}
    static void Main() {}
    static int Main(string[] args) {}
    static int Main() {}
    

    三、基础语句

    1、输入输出

    Console.WriteLine("人因梦想而伟大"); //输出后换行
    Console.Write(); //输出后不换行
    
    Console.ReadLine();//返回值为string类型
    Console.Read(); // 返回值为int类型
    

    四、编写规则

  5. 尽量使用接口编程

  6. 关键语句一定编写注释
  7. 局部变量随用随声明
  8. 尽量少用goto语句
  9. 如果参数多,建议使用结构
  10. 避免对大段代码使用 try…catch
  11. 同一个文件中避免编写多个类
  12. 字符串多变时,用StringBuilder
  13. if语句块中使用”{}”
  14. switch语句一定编写default

    五、OOP规则

    封装、继承、多态。