C# 静态类 自定义方法参数有this 关键字
public static class MyExtensions
{
//进行统计 对字符串内有空格 ,点,问号 进行分割
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
string s = "Hello Extension Methods? KA .+PO BU";
//可以直接调用自定义方法 不需要new 对象
int i = s.WordCount(); // i=6
这篇博客介绍了如何使用C#的静态类和扩展方法来统计字符串中包含特定字符(空格、点、问号)的单词数量。通过`WordCount`方法,可以直接在字符串实例上调用,无需创建对象,提高了代码的简洁性和可读性。
335

被折叠的 条评论
为什么被折叠?



