c# 3.0新特性之使用扩展方法

c# 3.0新特性之使用扩展方法
2009年8月18日 云飞扬 发表评论 阅读评论
-
Extension Methods 使用扩展方法
使用的时候需要注意的地方
1.扩展方法所属的类必须为静态非泛型类,扩展方法也是静态方法
2.第一个参数为被扩展的类型实例,并且必须用this进行修饰
3.第二个参数开始对对应被扩展类型实例所传递的参数列表,即扩展类型实例
传递的第一个参数对应扩展方法定义的第二个参数,依次类推
4.被扩展类型实例可像调用类型内部定义的实例方法一样调用扩展方法

这里定义一个扩展方法:
public static class Extensions
    {
        public static bool Compare(this Customer customer1, Customer customer2)
        {
            if (customer1.CustomerId == customer2.CustomerId &&
                customer1.Name == customer2.Name &&
                customer1.City == customer2.City)
            {
                return true;
            }

            return false;
        }

    }
其中Compare第一个参数用this修饰

 

完整源码例子,这个例子主要查询新建的newCustomer是否在列表List中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NewLanguageFeatures
{
    public class Customer
    {
        public int CustomerId { get; private set; }

        public string Name { get; set; }
        public string City { get; set; }

        public Customer(int Id)
        {
            CustomerId = Id;
        }

        public override string ToString()
        {
            return Name + "/t" + City + "/t" + CustomerId;
        }
    }
    public static class Extensions
    {
        public static bool Compare(this Customer customer1, Customer customer2)
        {
            if (customer1.CustomerId == customer2.CustomerId &&
                customer1.Name == customer2.Name &&
                customer1.City == customer2.City)
            {
                return true;
            }

            return false;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            var customers = CreateCustomers();

            var newCustomer = new Customer(10)
            {
                Name = "Stuart Glasson",
                City = "Oxford"
            };

            foreach (var c in customers)
            {
   
                if (newCustomer.Compare(c))
                {
                    Console.WriteLine("The new customer was already in the list");
                    return;
                }

            }

            Console.WriteLine("The new customer was not in the list");

        }

        static List<Customer> CreateCustomers()
        {
            return new List<Customer>
                {
                    new Customer(1) { Name = "Alex Roland",             City = "Berlin"    },
                    new Customer(2) { Name = "Oliver Cox",              City = "Marseille" },
                    new Customer(3) { Name = "Maurice Taylor",          City = "London"    },
                    new Customer(4) { Name = "Phil Gibbins",            City = "London"    },
                    new Customer(5) { Name = "Tony Madigan",            City = "Torino"    },
                    new Customer(6) { Name = "Elizabeth A. Andersen",   City = "Portland"  },
                    new Customer(7) { Name = "Justin Thorp",            City = "London"    },
                    new Customer(8) { Name = "Bryn Paul Dunton",        City = "Portland"  }
                };
        }
    }


本文来自: 本站内容欢迎转载,但是禁止去掉本文链接(转载无妨,去掉链接可耻!):http://www.ajaxcn.net/archives/180

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值