扩展方法和LINQ

以前一直看到方法列表中有种方法的图标是 普通方法的红立方体右边还有个箭头

就像这样 ,原来这种方法就是扩展方法

 

扩展方法不能访问它扩展的类型的私有成员。调用扩展方法只是调用静态方法的一种新语法

ExpandedBlockStart.gif代码
    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();
        }
    }

 

 这样就扩展了string类,This关键字区分了一般的静态方法和扩展方法

 

下面模仿.net类库的Enumerable类实现一个where方法

ExpandedBlockStart.gif代码
    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();
        }
    }

 如果连续用几个扩展方法

ExpandedBlockStart.gif代码
   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();
        }
    }

 

这是不是很像SQL语句?所以LINQ横空出世了。。

ExpandedBlockStart.gif代码
   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();
        }
    }

 

 

 

转载于:https://www.cnblogs.com/renjuwht/archive/2010/01/12/1644517.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值