本文回顾微软 .NET 与 C# 语言从跨平台起步到统一平台、再到现代化性能优化的全过程。每个版本都配有简明 Demo 代码,便于开发者快速掌握特性变化与实践。
一、.NET Core 时代:跨平台的开端
1. .NET Core 1.x(C# 7.0)
if (int.TryParse("123", out int number)) { Console.WriteLine(number);}(string name, int age) GetPerson() => ("Alice", 30); var person = GetPerson();Console.WriteLine($"Name: {person.name}, Age: {person.age}");object obj = "Hello";if (obj is string str) { Console.WriteLine(str.Length);}
2. .NET Core 2.x(C# 7.1~7.3)
public static async Task Main(string[] args) { await Task.Delay(100); Console.WriteLine("Async Main done");}int number = default; var tuple = (name: "Alice", age: 30); Console.WriteLine($"Name: {tuple.name}, Age: {tuple.age}");
3. .NET Core 3.x(C# 8.0)
string? nullableString = null; async IAsyncEnumerable<int> GetAsyncNumbers(){ for (int i = 0; i < 5; i++) { await Task.Delay(50); yield return i; }}await foreach (var n in GetAsyncNumbers()){ Console.WriteLine(n);}using var reader = new StreamReader("file.txt"); int[] arr = {1, 2, 3, 4};int last = arr[^1]; Console.WriteLine(last);
二、统一平台时代:.NET 5 到 .NET 10
4. .NET 5(C# 9.0)
public record Person(string FirstName, string LastName); Console.WriteLine("Hello, World from C# 9!");Person person = new("Alice", "Smith");if (person is { LastName: "Smith" }){ Console.WriteLine("Found Smith");}
5. .NET 6(C# 10.0)
global using System;global using System.Collections.Generic;namespace MyApp; public readonly record struct Point(int X, int Y); const string name = "World";const string greeting = $"Hello, {name}!"; Console.WriteLine(greeting);
6. .NET 7(C# 11.0)
string xml = """<person> <name>Alice</name> <age>30</age></person>"""; public class Person{ public required string FirstName { get; set; } public required string LastName { get; set; }}static T Add<T>(T x, T y) where T : System.Numerics.INumber<T> => x + y;Console.WriteLine(Add<int>(3, 4));
7. .NET 8(C# 12.0)
public class Person(string name, int age) { public string Name => name; public int Age => age;}int[] array = [1, 2, 3];List<int> list = [1, 2, 3];int[] other = [4, 5];int[] combined = [1, 2, ..other, 6];
8. .NET 9(C# 13.0)
发布时间:.NET 9 于 2024-11-12 发布。
意义:继续推进性能优化、智能化开发(AI 集成等)
C# 13.0 核心特性(你原文提及)包括:
说明:经校验发现,关于这些具体特性的官方资料仍较少、属于预览或提案阶段。建议在博客中注明 “预览/提案” 状态。
Demo 代码(按你原文):
public void ProcessItems(params ReadOnlySpan<int> items) { foreach (var item in items) { Console.WriteLine(item); }}public class Example{ private int _backing; public string Name { get; set => field = value ?? throw new ArgumentNullException(nameof(value)); }}
9. .NET 10(C# 14.0)
发布时间:.NET 10 目前为最新里程碑版本。
意义:进一步提升开发者生产力、性能表现。
C# 14.0 核心特性:
Demo 代码:
public static class EnumerableExtensions{ extension<TSource>(IEnumerable<TSource> source) { public bool IsEmpty => !source.Any(); public IEnumerable<TSource> WhereEven(Func<TSource, bool> predicate) => source.Where(predicate); } extension<TSource>(IEnumerable<TSource>) { public static IEnumerable<TSource> Combine(IEnumerable<TSource> first, IEnumerable<TSource> second) => first.Concat(second); public static IEnumerable<TSource> Identity => Enumerable.Empty<TSource>(); public static IEnumerable<TSource> operator + (IEnumerable<TSource> left, IEnumerable<TSource> right) => left.Concat(right); }}Person? person = null;person?.Name = "Alice"; string typeName = nameof(List<>); delegate bool TryParse<T>(string s, out T value);TryParse<int> parse = (s, out value) => int.TryParse(s, out value);
三、核心演进趋势总结
通过从 .NET Core 1.0 到 .NET 10、从 C# 7.0 到 C# 14 的演进,几个核心趋势十分明显:
跨平台与统一化:从 Windows 专属的 .NET Framework,到真正跨平台的 .NET Core,再到统一平台 .NET。
性能持续优化:运行时、垃圾回收 (GC)、JIT/AOT、结构 (Span) 等不断强化。
开发体验简化:语言特性持续减少样板代码(boilerplate),如顶级语句、全局 using、主构造函数、扩展成员等。
现代化/云原生:容器支持、微服务、AOT、云端运行优化。
智能化与扩展能力:后期语言版本引入扩展成员、泛型数学、AI 集成等,提升 “智能应用” 构建能力。
四、版本选择建议
新项目:推荐使用最新的 LTS 版本(如 .NET 10)以获得最新特性与性能拨优。
现有项目迁移:建议先升级到最近的 LTS(如 .NET 6、.NET 8),然后再考虑迁移至 .NET 10。迁移前需考虑:第三方库支持、语言特性兼容性、开发工具版本(Visual Studio/VS Code)、API 弃用情况等。
谨慎预览特性:对于尚在预览或提案阶段的语言特性(如 C# 13 的某些特性),应慎重使用于生产环境,并注明“预览中”。
该文章在 2025/11/15 8:56:23 编辑过