前言
C# 各个版本的语法糖随着语言的更新逐渐演变。语法糖是为了简化代码而设计的,使得开发者能够更快速、简洁地完成某些常见的操作。下面我将按 C# 版本介绍一些主要的语法糖,并给出对应的示例代码。
1. C# 1.0: 基本语法
C# 1.0 引入了面向对象编程的基础语法,包含类、对象、继承等基本结构。
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
var person = new Person("John", 30);
Console.WriteLine(person.Name);
2. C# 2.0: 泛型和匿名方法
2.1 泛型
泛型使得代码更加灵活,避免了使用不安全的类型转换。
List<int> numbers = new List<int> { 1, 2, 3, 4 };
foreach (var number in numbers)
{
Console.WriteLine(number);
}
2.2 匿名方法
匿名方法允许你直接在代码中定义方法,而无需显式声明一个方法名。
Func<int, int, int> add = delegate(int x, int y) { return x + y; };
Console.WriteLine(add(3, 4));
3. C# 3.0: 自动属性、匿名类型、扩展方法、LINQ
3.1 自动属性
C# 3.0 引入了自动属性的概念,简化了属性的定义。
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
3.2 匿名类型
匿名类型允许你在没有明确类型定义的情况下创建一个对象。
var person = new { Name = "John", Age = 30 };
Console.WriteLine(person.Name);
3.3 LINQ(语言集成查询)
LINQ 提供了更简洁的方式来操作集合数据。
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
evenNumbers.ForEach(Console.WriteLine); // 输出 2, 4
3.4 扩展方法
扩展方法允许你给现有类型添加方法,而不需要修改它们。
public static class StringExtensions
{
public static bool IsCapitalized(this string str)
{
return char.IsUpper(str[0]);
}
}
Console.WriteLine("Hello".IsCapitalized());
4. C# 4.0: 动态类型、命名参数和可选参数
4.1 动态类型
dynamic 类型允许你绕过静态类型检查,类似于 JavaScript。
dynamic value = 10;
Console.WriteLine(value.GetType());
value = "Hello";
Console.WriteLine(value.GetType());
4.2 命名参数和可选参数
C# 4.0 引入了命名参数和可选参数,简化了方法调用。
public void DisplayInfo(string name, int age = 18)
{
Console.WriteLine($"Name: {name}, Age: {age}");
}
DisplayInfo(name: "John");
DisplayInfo("Jane", age: 25);
5. C# 5.0: 异步编程
5.1 async 和 await
C# 5.0 引入了 async 和 await,使得异步编程变得更加简洁。
public async Task<int> CalculateSumAsync(int a, int b)
{
await Task.Delay(1000);
return a + b;
}
public async Task Run()
{
int result = await CalculateSumAsync(5, 7);
Console.WriteLine(result);
}
6. C# 6.0: 表达式体成员、字符串插值
6.1 表达式体成员
C# 6.0 引入了表达式体成员,让 getter、setter 或方法体更加简洁。
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Description => $"{Name} is {Age} years old";
}
var person = new Person { Name = "John", Age = 30 };
Console.WriteLine(person.Description);
6.2 字符串插值
C# 6.0 引入了字符串插值,简化了字符串拼接。
string name = "John";
int age = 30;
Console.WriteLine($"Name: {name}, Age: {age}");
7. C# 7.0: 值元组
7.1 值元组
C# 7.0 引入了值元组,使得返回多个值变得更简单。
public (string Name, int Age) GetPersonInfo()
{
return ("John", 30);
}
var person = GetPersonInfo();
Console.WriteLine(person.Name);
Console.WriteLine(person.Age);
8. C# 8.0: 异步流、空值合并赋值操作符
8.1 异步流
C# 8.0 引入了异步流,使得处理异步数据流变得更方便。
public async IAsyncEnumerable<int> GetNumbersAsync()
{
for (int i = 0; i < 5; i++)
{
await Task.Delay(1000);
yield return i;
}
}
public async Task Run()
{
await foreach (var number in GetNumbersAsync())
{
Console.WriteLine(number);
}
}
8.2 空值合并赋值操作符
C# 8.0 引入了空值合并赋值操作符 ??=,简化了对空值的检查和赋值。
string? name = null;
name ??= "Default Name";
Console.WriteLine(name);
9. C# 9.0: 记录类型、初始化器
9.1 记录类型
C# 9.0 引入了记录类型,它是一个专门用来存储数据的类型,且具有不可变的特性。
public record Person(string Name, int Age);
var person = new Person("John", 30);
Console.WriteLine(person.Name);
9.2 初始化器
C# 9.0 提供了更简洁的方式来创建和初始化对象。
Person person = new() { Name = "John", Age = 30 };
Console.WriteLine(person.Name);
10. C# 10.0: 文件作用域的命名空间
10.1 文件作用域的命名空间
C# 10.0 引入了文件作用域命名空间,使得代码更简洁。
namespace MyNamespace;
public class Person
{
public string Name { get; set; }
}
总结
C# 的每个版本都带来了许多改进,语法糖的不断增加使得代码更加简洁、易读且高效。从 C# 1.0 到 C# 10.0,语言的演进显著提高了开发体验,并解决了许多开发中的常见问题。通过掌握这些语法糖,开发者能够写出更加简洁、清晰的代码。
阅读原文:原文链接
该文章在 2025/8/8 13:08:37 编辑过