惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
H
Help Net Security
GbyAI
GbyAI
博客园_首页
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
月光博客
月光博客
美团技术团队
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - 叶小钗
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed

博客园 - YellowWee(端木柒)

Founders at Work: Stories of Startups' Early Days 创业初期的故事 生成自己站点的SiteMap 开通新博客,欢迎大家访问:http://www.yellowwee.com.cn [转载]Fix Your Site With the Right DOCTYPE Java Top Books 自定义DataContext类 [转载]Encrypting Configuration Information in ASP.NET 2.0 Applications 安装 Sql Server Query Visualizer - YellowWee(端木柒) 生活的五项调整 《架构师杂志》评述:Scott Guthrie 转自MSDN WPF/Every CTP 发布 使用虚拟机安装vista RTM 配置.net 3.0开发环境 如何在TableAdapter中使用Data Access Application Block的疑问?? 今年的 Jolt 大奖 胡汉三 归来 IssueVision 学习笔记(三)-----设计模式之OBSERVER(观察者)模式 IssueVision 学习笔记(二)-----为控件添加自定义属性和事件 IssueVision 学习笔记(一)-----使用SoapHeader传递Web Serivices自定义的身份验证数据
C# 中的扩展方法---Extension methods in C#
YellowWee(端木柒) · 2007-08-21 · via 博客园 - YellowWee(端木柒)

Posted on 2007-08-21 11:16  YellowWee(端木柒)  阅读(360)  评论()    收藏  举报

使用扩展方法需要注意的几点:

    1.  The method is define in a top level static class ( the class is directly under the namespace)
    2. The method is static and decorates its first param with a new param modifier this, this param is called the "instance parameter" and its an error to use the "this" modifiers on any other parameter.
    3. No other parameter modifiers ( ref, out etc) are allowed with "this" (so values types can't be passed by reference to an extension methods, VB will allow ref).
    4. The instance parameter can't be a pointer type.
    5. The method is public (its accessible to anyone who can get to its parentclass).
    6. The Type parameter used must be defined on the method and not the parentclass.
    7.  The instance parameter cannot have the type of the Type parameter.

Sample:

 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5
 6namespace NewFeatures
 7{
 8    public static class Extensions
 9    {
10        public static List<T> Append<T>(this List<T> a, List<T> b)
11        {
12            var newList = new List<T>(a);
13            newList.AddRange(b);
14            return newList;
15        }

16
17        public static bool Compare(this Customer customer1, Customer customer2)
18        {
19            if (customer1.Name == customer2.Name && customer1.City == customer2.City)
20                return true;
21            return false;
22        }

23    }

24
25    public class Customer
26    {
27        public string Name getset; }
28        public string City getset; }
29
30        public override string ToString()
31        {
32            return Name + "\t" + City;
33        }

34    }

35
36    class Program
37    {
38        static void Main(string[] args)
39        {
40            var customers = CreateCustomers();
41
42            var newCustomer = new Customer()
43            {
44                Name = "Liz Nixon",
45                City = "Portland"
46            }
;
47
48            var addedCustomers = new List<Customer>
49            {
50                new Customer() {Name = "Paolo Accorti",City = "Torino"},
51                new Customer () {Name  = "Diego Roel",City = "Madrid"}
52            }
;
53
54            var updateCustomers = customers.Append(addedCustomers);
55
56            foreach (var c in updateCustomers)
57            {
58                Console.WriteLine(c.ToString());
59
60                if (newCustomer.Compare(c))
61                {
62                    Console.WriteLine("already in");
63                    //return;
64                }

65            }

66
67            Console.WriteLine("not in");
68
69            foreach (var c in FindCustomersByCity(customers, "London"))
70                Console.WriteLine(c);
71        }

72
73        public static List<Customer> FindCustomersByCity(List<Customer> customers, string city)
74        {
75            return customers.FindAll(c => c.City == city);
76        }

77
78        static List<Customer> CreateCustomers()
79        {
80            return new List<Customer>
81            {
82        new Customer() { Name = "Maria Anders",     City = "Berlin"    },
83        new Customer() { Name = "Laurence Lebihan", City = "Marseille" },
84        new Customer() { Name = "Elizabeth Brown",  City = "London"    },
85        new Customer() { Name = "Ann Devon",        City = "London"    },
86        new Customer() { Name = "Paolo Accorti",    City = "Torino"    },
87        new Customer() { Name = "Fran Wilson",      City = "Portland"  },
88        new Customer() { Name = "Simon Crowther",   City = "London"    },
89        new Customer() { Name = "Liz Nixon",          City = "Portland"  }
90    }
;
91        }

92
93    }

94}

95