扩展方法
扩展方法其行为类似于某种类型(类、接口、结构体、原始值或枚举)的实例方法,目的为了增加原本类型不支持或需要多步操作的功能,所以扩展类命名一般为xxxExtensions。
例如下面代码
public static class StringExtension
{
public static int WordCount(this String str)
{
return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
class Program
{
static void Main(string[] args)
{
string s = "The quick brown fox jumped over the lazy dog.";
int i = s.WordCount();
System.Console.WriteLine("Word count of s is {0}", i);
}
}
StringExtension就是针对string类扩展了WordCount(this String str)方法,this 为此方法的关键字,指明此扩展为String类型,区分了扩展方法和一般的静态方法。
扩展类对扩展类型有几点说明:
1.这个类必须对外部可见,不能是Protected、private、internal等。
2.将扩展方法实现为静态方法,并且可见性至少与所在类可见性相同。
3.扩展方法的第一个参数为指定方法所操作的类型,参数前面必须加上this修饰符。
4.调用代码时,可添加using指定包含扩展方法类的命名空间。
5.调用方法跟调用类型的实例方法一样。
调用时不需要传入类型参数,可从arg1参数开始
WordCount(this String str, int arg1)
对应调用方法
s.WordCount(1);
这个扩展还可以用在哪里呢?例如下面代码
public static bool IsNullOrEmpty(this ICollection i)
{
return i == null || i.Count == 0;
}
这样就可以方便的判断那些继承了ICollection接口的数据结构,Array、List、Dictionary等,都可以直接使用这个方法。
总结
扩展方法可以写入最初没有提供该方法的类中,还可以把方法添加到实现一个接口的任何类中,例如修改成(this IList i),这样每个继承了IList 接口的类都可以使用这个扩展方法。