YellowWee's Scripts

妳的世界能夠從此不同...而我的世界... 亦因妳而改變...

导航

C# 中的扩展方法---Extension methods in C#

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



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

    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



参阅 :Sree's ventures Extension method in C#