C# 扩展方法只会写 this 吗?C# 14 新语法直接把扩展方法玩出了花

前言

PS: 不是,AI编程都普及了,怎么还有人研究编程语言的语法?

开个玩笑哈哈,一些简单和定制性比较强的代码还是手写比较好,有和AI对话的时间,我都把代码写好了😄

最近我在给 StarBlog 写一些分析脚本,需要用到扩展方法来简化代码,我想到前段时间看到 .NET10 的更新文档说有新的扩展方法语法,我本来以为是和 Kotlin 一样不再需要写一个静态类了,结果还是要...

不过新的写法还挺好用的。

PS: 虽然代码是自己写的,但文档不用自己看了,直接让AI总结😄

正好借本文记录一下这个新的写法。

从静态方法到扩展块

传统的扩展方法需要每个方法都重复写 this 参数,且只能扩展方法。新语法通过 extension 关键字定义一个块,将目标类型集中声明。

传统写法是这样的

c# 复制代码
public static class StringExtensions {
    // 每个方法都要写一遍 (this string str)
    public static bool IsValidEmail(this string str) => str.Contains("@");
    public static string ToTitleCase(this string str) => ...;
}

新语法(C# 14 / .NET 10)

c# 复制代码
public static class MyExtensions {
    // 定义一个针对 string 的扩展块,str 是接收者名称
    extension(string str) {
        // 1. 扩展属性 (以前做不到)
        public bool IsValidEmail => str.Contains("@");

        // 2. 扩展方法 (不再需要写 static 和 this)
        public string ToTitleCase() => char.ToUpper(str[0]) + str.Substring(1);
    }
}

新语法支持的成员类型

这是 .NET 10 最强大的地方,扩展的能力范围极大地拓宽了:

成员类型 是否支持 示例语法
实例方法 public void M() => ...
实例属性 public int Length => str.Length;
静态方法 public static void Help() => ...
静态属性 public static string Default => "None";
运算符重载 public static bool operator +(string a, MyType b)
索引器 public char this[int i] => str[i];

静态扩展成员

以前我们无法给一个现有的类(比如 DateTimeint)添加静态工具方法,现在可以了。

c# 复制代码
public static class DateTimeExtensions {
    // 注意:括号内只有类型,没有实例名称,表示静态扩展
    extension(DateTime) {
        public static DateTime Tomorrow => DateTime.Now.AddDays(1);
    }
}

// 使用时:
var nextDay = DateTime.Tomorrow; // 就像 DateTime 原生自带的一样

泛型支持

新语法对泛型的支持非常优雅,尤其是在处理集合(LINQ 风格)时:

c# 复制代码
public static class EnumerableExtensions {
    extension<T>(IEnumerable<T> source) {
        // 扩展属性:判断集合是否为空
        public bool IsEmpty => !source.Any();
        
        // 扩展方法
        public void ForEach(Action<T> action) {
            foreach (var item in source) action(item);
        }
    }
}

实际代码

既然说到是给 StarBlog 写的脚本,那我也把实际用到的代码放出来吧 (水字数(bushi)

实际上是一个提取 markdown 二级标题下段落的扩展方法

C# 复制代码
public static class MyExtensions {
    extension(Post post) {
        public string GetSection(string title) {
            return ExtractSectionRegex(post.Content, title);
        }

        public Dictionary<string, string> GetSections(params string[] titles) {
            var dict = new Dictionary<string, string>();
            foreach (var title in titles) {
                dict.Add(title, post.GetSection(title));
            }

            return dict;
        }
    }

    private static string ExtractSectionRegex(string content, string title) {
        // 匹配目标标题开始,直到下一个标题或文档末尾
        string pattern = $@"##\s*{title}\s*([\s\S]*?)(?=\n##|$)";
        var match = Regex.Match(content, pattern);
        return match.Success ? match.Groups[1].Value.Trim() : string.Empty;
    }
}

实际上这个 GetSections 方法又可以用 LINQ 语法简化一下

c# 复制代码
public Dictionary<string, string> GetSections(params string[] titles) {
    return titles.ToDictionary(title => title, title => post.GetSection(title));
}

再用 method group 简化一下,最终效果

c# 复制代码
public Dictionary<string, string> GetSections(params string[] titles) {
    return titles.ToDictionary(title => title, post.GetSection);
}

翻译翻译,什么叫优雅?!🐸

小结

新的扩展语法糖还是很甜的,在我看来有几个优势:

  • 代码更自然:扩展属性让 API 看起来更像目标类型的原生一部分,而不是非要调用一个带括号的方法。
  • 逻辑集中 :在一个 extension 块里定义所有相关的扩展,代码结构比散落在静态类里的方法更清晰。
  • 零破坏性 :新语法与旧的 this 扩展方法语法完全兼容,可以根据喜好混合使用。

注意: 只有安装了 .NET 10 SDK 并在项目文件中将 <LangVersion> 设置为 14.0preview,才能使用这些新特性。

所以赶紧把项目都升级到 .NET10 把😄

PS: 我这个 StarBlog 项目一路从 .NET6 升级到 .NET10,也算是有点历史的项目了🤣

相关推荐
唐青枫4 小时前
C#.NET SignalR 深入解析:实时通信、Hub 与连接管理实战
c#·.net
唐宋元明清218810 小时前
.NET Win32磁盘动态卷/跨区卷触发“函数不正确”问题排查
windows·c#·存储
hez201010 小时前
Satori GC:同时做到高吞吐、低延时和低内存占用
c#·.net·.net core·gc·clr
唐青枫1 天前
C#.NET Channel 深入解析:高性能异步生产者消费者模型实战
c#·.net
小峥降临2 天前
Rokid UXR 的手势追踪虚拟中更真实的手实战开发【含 工程源码 和 最终完成APK】
c#
晨星shine6 天前
GC、Dispose、Unmanaged Resource 和 Managed Resource
后端·c#
用户298698530146 天前
.NET 文档自动化:Spire.Doc 设置奇偶页页眉/页脚的最佳实践
后端·c#·.net
用户3667462526746 天前
接口文档汇总 - 2.设备状态管理
c#
用户3667462526746 天前
接口文档汇总 - 3.PLC通信管理
c#