这是一个简单的字数扩展方法。
public static class StringExtension
{
public static int WordCount(this string s)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] != ' ')
{
if ((i + 1) == s.Length)
{
count++;
}
else
{
if (s[i + 1] == ' ')
{
count++;
}
}
}
}
return count;
}
}
告诉我为什么扩展方法是静态的,为什么它被包装在静态类中?