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

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - 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