以前一直看到方法列表中有种方法的图标是 普通方法的红立方体右边还有个箭头
就像这样 ,原来这种方法就是扩展方法
扩展方法不能访问它扩展的类型的私有成员。调用扩展方法只是调用静态方法的一种新语法


public static class A
{
public static void foo(this string s)
{
Console.WriteLine("test:{0}", s);
}
}
class Program
{
static void Main(string[] args)
{
string s = "hello";
s.foo();
Console.Read();
}
}
{
public static void foo(this string s)
{
Console.WriteLine("test:{0}", s);
}
}
class Program
{
static void Main(string[] args)
{
string s = "hello";
s.foo();
Console.Read();
}
}
这样就扩展了string类,This关键字区分了一般的静态方法和扩展方法
下面模仿.net类库的Enumerable类实现一个where方法


public static class A
{
//扩展方法,这样实现了IEnumerable<T>接口的类都可以调用这个扩展方法了
public static IEnumerable<T> where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
foreach (T item in source)
{
//用第二个参数泛型委托
if (predicate(item))
{
yield return item;
}
}
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("aaaa");
list.Add("baaa");
list.Add("baaa");
list.Add("caaa");
list.Add("afds");
list.Add("awww");
list.Add("attt");
//使用上面自己定义的拓展方法,筛选以"a"开头的字符串
foreach (string s in list.where(a => a.StartsWith("a")))
{
Console.WriteLine(s);
}
//这是.net类库自带的方法,排序
foreach (string s in list.OrderByDescending(a => a))
{
Console.WriteLine(s);
}
//这是.net类库自带的方法
foreach (string s in list.Select(a => a + "111111"))
{
Console.WriteLine(s);
}
Console.Read();
}
}
{
//扩展方法,这样实现了IEnumerable<T>接口的类都可以调用这个扩展方法了
public static IEnumerable<T> where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
foreach (T item in source)
{
//用第二个参数泛型委托
if (predicate(item))
{
yield return item;
}
}
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("aaaa");
list.Add("baaa");
list.Add("baaa");
list.Add("caaa");
list.Add("afds");
list.Add("awww");
list.Add("attt");
//使用上面自己定义的拓展方法,筛选以"a"开头的字符串
foreach (string s in list.where(a => a.StartsWith("a")))
{
Console.WriteLine(s);
}
//这是.net类库自带的方法,排序
foreach (string s in list.OrderByDescending(a => a))
{
Console.WriteLine(s);
}
//这是.net类库自带的方法
foreach (string s in list.Select(a => a + "111111"))
{
Console.WriteLine(s);
}
Console.Read();
}
}
如果连续用几个扩展方法


class Program
{
static void Main(string[] args)
{
List<string> names = new List<string> { "wht", "cb", "ra", "ca" };
IEnumerable<string> result = names.Where(n => n.StartsWith("c")).
OrderByDescending(n => n).
Select(n => n);
foreach (string n in result)
{
Console.WriteLine(n);
}
Console.Read();
}
}
{
static void Main(string[] args)
{
List<string> names = new List<string> { "wht", "cb", "ra", "ca" };
IEnumerable<string> result = names.Where(n => n.StartsWith("c")).
OrderByDescending(n => n).
Select(n => n);
foreach (string n in result)
{
Console.WriteLine(n);
}
Console.Read();
}
}
这是不是很像SQL语句?所以LINQ横空出世了。。


class Program
{
static void Main(string[] args)
{
List<string> names = new List<string> { "wht", "cb", "ra", "ca" };
var nameAsC = from n in names
where n.StartsWith("c")
orderby n descending
select n;
foreach (string name in nameAsC)
{
Console.WriteLine(name);
}
//Where(n => n.StartsWith("c"))被翻译为where n.StartsWith("c")
//OrderByDescending(n => n) 被翻译为orderby n descending
//Select(n => n) 被翻译为select n
Console.Read();
}
}
{
static void Main(string[] args)
{
List<string> names = new List<string> { "wht", "cb", "ra", "ca" };
var nameAsC = from n in names
where n.StartsWith("c")
orderby n descending
select n;
foreach (string name in nameAsC)
{
Console.WriteLine(name);
}
//Where(n => n.StartsWith("c"))被翻译为where n.StartsWith("c")
//OrderByDescending(n => n) 被翻译为orderby n descending
//Select(n => n) 被翻译为select n
Console.Read();
}
}