C# 基本语法详解(C# Basic Syntax)
                           
天天向上
发布: 2025-04-06 23:24:55

原创
774 人浏览过

📌 目录结构

  1. C# 基本语法概览
  2. 程序结构复习
  3. 数据类型(Data Types)
  4. 变量和常量
  5. 运算符
  6. 条件语句
  7. 循环结构
  8. 方法(函数)定义与调用
  9. 类与对象的基本使用
  10. 注释风格
  11. 命名规范
  12. 权威链接与官方文档

1. C# 基本语法概览

C# 是一种 强类型、面向对象 的语言,语法类似 Java 和 C++。主要运行在 .NET 平台,用于开发 Web、桌面、移动、游戏等应用。


2. 程序结构复习(Hello World 示例)

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}
  • using:导入命名空间
  • class Program:类是 C# 程序的基本单元
  • static void Main:主入口方法
  • Console.WriteLine():控制台输出

3. 数据类型(Data Types)

➤ 值类型(Value Types)

类型示例
int整数,如 123
float单精度浮点数
double双精度浮点数
char单个字符 'A'
bool布尔值 true/false
decimal高精度货币计算

➤ 引用类型(Reference Types)

类型示例
string字符串 "Hello"
object万能基类
自定义类Person, Car

4. 变量与常量

int age = 25;              // 变量
const double PI = 3.14159; // 常量
string name = "Alice";
bool isAdmin = true;

5. 运算符(Operators)

类型示例
算术运算符+ - * / %
比较运算符== != > < >= <=
逻辑运算符&& || !
赋值运算符= += -= *=
int x = 10;
int y = 5;
int sum = x + y;
bool isGreater = x > y;

6. 条件语句

➤ if-else

if (x > 0)
{
    Console.WriteLine("Positive");
}
else
{
    Console.WriteLine("Non-positive");
}

➤ switch-case

int day = 3;

switch (day)
{
    case 1:
        Console.WriteLine("Mon");
        break;
    case 2:
        Console.WriteLine("Tue");
        break;
    default:
        Console.WriteLine("Other");
        break;
}

7. 循环结构

➤ for 循环

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}

➤ while 循环

int i = 0;
while (i < 5)
{
    Console.WriteLine(i);
    i++;
}

➤ foreach(适用于集合)

string[] fruits = { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

8. 方法定义与调用

static int Add(int a, int b)
{
    return a + b;
}

static void Main(string[] args)
{
    int result = Add(3, 4);
    Console.WriteLine(result); // 输出 7
}

9. 类与对象的基本使用

class Person
{
    public string Name;
    public int Age;

    public void Greet()
    {
        Console.WriteLine($"Hi, I'm {Name}, {Age} years old.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person p = new Person();
        p.Name = "Alice";
        p.Age = 30;
        p.Greet();
    }
}

10. 注释风格

// 单行注释

/*
 多行注释
 可以写多行
*/

/// <summary>
/// 用于生成 XML 文档的注释
/// </summary>
public void MyMethod() {}

11. 命名规范(Coding Conventions)

项目推荐方式
类名PascalCase
方法名PascalCase
变量名camelCase
常量名UPPER_SNAKE_CASE
命名空间名公司.项目.模块

📘 官方命名规范:
👉 C# Coding Conventions – Microsoft


12. 官方权威文档与学习路径

内容链接
C# 语法基础https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/
数据类型https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types
控制流https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/selection-statements
方法定义https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods
类与对象https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

总结回顾

内容核心知识点
数据类型值类型 vs 引用类型
控制结构if、switch、for、while、foreach
方法参数传递、返回值
面向对象类、字段、方法
命名PascalCase / camelCase
发表回复 0

Your email address will not be published. Required fields are marked *