using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("a,b,c".Split(",").Length);
Console.ReadKey();
}
}
/// <summary>
/// 扩展方法类
/// </summary>
static class Extensions
{
/// <summary>
/// 使用指定分隔符分割字符串,返回一字符串数组
/// </summary>
/// <param name="str"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static string[] Split(this string str, string separator)
{
return str.Split(new string[] { separator }, StringSplitOptions.None);
}
}
}扩展方法也可以IntelliSense,效果如下:

说明
·扩展方法必需在.NET Framework3.5及以上版本中可以用,使用时需引用System.Core.dll;
·扩展方法所在的类及方法都必需是静态的;
·扩展方法第一个参数必须带this,this后边为要扩展的类名;
·当扩展方法的签名和类原有的方法签名重复时,.NET优先使用类自带的方法;
资料
@.扩展方法:http://msdn.microsoft.com/zh-cn/library/bb383977(v=VS.90).aspx
评论(0)
暂无评论。