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

推荐订阅源

GbyAI
GbyAI
Vercel News
Vercel News
F
Fortinet All Blogs
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Engineering at Meta
Engineering at Meta
爱范儿
爱范儿
V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
U
Unit 42
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
S
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
Attack and Defense Labs
Attack and Defense Labs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hacker News: Ask HN
Hacker News: Ask HN
Jina AI
Jina AI
Schneier on Security
Schneier on Security
W
WeLiveSecurity
Microsoft Security Blog
Microsoft Security 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