我给你整理了最核心、最常用、必须掌握的 C# 语法,直接对标 .NET Core 开发,全是实战高频知识点,新手一看就懂、一用就会。
一、基础结构(程序入口)
.NET Core 控制台/项目的固定骨架,所有程序都从这里开始:
// 命名空间(相当于文件夹,分类管理代码)
namespace MyFirstApp
{
// 类(代码的容器)
class Program
{
// 主方法:程序唯一入口,固定写法
static void Main(string[] args)
{
// 你的代码写在这里
System.Console.WriteLine("Hello .NET Core!");
}
}
}
二、变量与数据类型
1. 常用值类型(直接存数据)
// 整数
int age = 20;
// 小数(带d)
double score = 95.5;
// 布尔值(真/假)
bool isStudent = true;
// 单个字符(单引号)
char gender = '男';
// 十进制(财务专用,精度最高)
decimal money = 1000.50m;
2. 引用类型(存地址,最常用)
// 字符串(双引号,必备)
string name = "张三";
// 空值
string nullStr = null;
3. 变量声明语法
// 方式1:指定类型(推荐)
int num = 10;
// 方式2:var自动推断类型(简化写法)
var name = "李四"; // 自动识别为string
三、控制台输入输出(调试必备)
// 输出(换行)
Console.WriteLine("输出内容");
// 输出(不换行)
Console.Write("不换行");
// 输入(读取一行文本)
string input = Console.ReadLine();
// 输入转数字
int num = int.Parse(Console.ReadLine());
四、运算符
- 算术运算符:
+ - * / %(加减乘除、取余) - 比较运算符:
> < >= <= == !=(等于、不等于) - 逻辑运算符:
&&(且)、||(或)、!(非)
int a = 10, b = 3;
bool result = a > b && a != 0; // true
五、流程控制(核心逻辑)
1. if-else 条件判断
int score = 85;
if (score >= 90)
{
Console.WriteLine("优秀");
}
else if (score >= 60)
{
Console.WriteLine("及格");
}
else
{
Console.WriteLine("不及格");
}
2. switch 多分支
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("周一");
break;
case 2:
Console.WriteLine("周二");
break;
default:
Console.WriteLine("其他");
break;
}
3. 循环语句
for 循环(固定次数)
// 输出1-10
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
while 循环(条件循环)
int i = 1;
while (i <= 5)
{
Console.WriteLine(i);
i++;
}
六、数组(存储多个数据)
// 1. 定义并赋值
int[] nums = { 1, 2, 3, 4, 5 };
// 2. 定义长度
string[] names = new string[3];
names[0] = "张三"; // 下标从0开始
// 遍历数组
foreach (int num in nums)
{
Console.WriteLine(num);
}
七、方法(函数,代码复用)
1. 无返回值方法(void)
// 定义
static void SayHello()
{
Console.WriteLine("你好");
}
// 调用
SayHello();
2. 有返回值方法
// 定义:int是返回类型,a/b是参数
static int Add(int a, int b)
{
return a + b; // 返回结果
}
// 调用
int sum = Add(10, 20); // sum=30
3. 方法重载(同名不同参数)
static int Add(int a, int b) { }
static double Add(double a, double b) { }
八、面向对象(C# 核心)
1. 类与对象
// 定义类
class Person
{
// 字段/属性
public string Name { get; set; }
public int Age { get; set; }
// 方法
public void SayHi()
{
Console.WriteLine($"我是{Name},{Age}岁");
}
}
// 创建对象(实例化)
Person p = new Person();
p.Name = "张三";
p.Age = 20;
p.SayHi();
2. 构造方法(初始化对象)
class Person
{
public string Name { get; set; }
// 构造方法:类名相同,无返回值
public Person(string name)
{
Name = name;
}
}
// 直接初始化
Person p = new Person("张三");
3. 封装、继承、多态(极简版)
// 继承:Student 继承 Person
class Student : Person
{
public int Score { get; set; }
}
// 多态:重写方法
class Person
{
public virtual void SayHi()
{
Console.WriteLine("我是人");
}
}
class Student : Person
{
public override void SayHi()
{
Console.WriteLine("我是学生");
}
}
九、集合(比数组更好用)
1. List 动态列表(最常用)
// 创建
List<string> list = new List<string>();
// 添加元素
list.Add("苹果");
list.Add("香蕉");
// 遍历
foreach (var item in list)
{
Console.WriteLine(item);
}
2. Dictionary 键值对
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "张三");
// 根据键取值
string name = dict[1];
十、异常处理(避免程序崩溃)
try
{
// 可能报错的代码
int num = int.Parse("abc");
}
catch (Exception ex)
{
// 捕获错误
Console.WriteLine("出错了:" + ex.Message);
}
finally
{
// 无论对错都执行
Console.WriteLine("执行完毕");
}
十一、命名规范(必遵守)
- 类名、方法名:大驼峰(
Person、GetName) - 变量名、参数名:小驼峰(
age、userName) - 常量:全大写+下划线(
MAX_COUNT)
总结
- 程序入口:
Main方法是起点,固定写法; - 核心语法:变量、判断、循环、方法是基础;
- 核心思想:面向对象(类、对象、继承、多态);
- 常用工具:
List、Dictionary、异常处理。
这些就是 .NET Core C# 开发必须掌握的核心语法,学会这些就能写 80% 的业务代码!