Essential Language Features
扩展方法:
1、下面是通过静态类的静态属性以及this来实现单独一个类的方法来的扩展
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
using System.Collections.Generic;
public class Product
{
public int ProductID { get; set; }
private string name;
public string Description { get; set;}
public decimal Price { get; set; }
public string Category { set; get;}
public string Name
{
get { return ProductID + name;}
set { name = value; }
}
}
public class ShoppingCart
{
public List<Product> Products { get; set; }
}
/// <summary>
/// 下面的参数是带this的,类是静态类,怎么扩展一个类的方法
/// </summary>
public static class MyExtensionMethods
{
public static decimal TotalPrices(this ShoppingCart cartParam)
{
decimal total = 0;
foreach (Product prod in cartParam.Products)
{
total += prod.Price;
}
return total;
}
}
class Program
{
static void Main(string[] args)
{
// create and populate ShoppingCart
ShoppingCart cart = new ShoppingCart
{
Products = new List<Product>
{
new Product {Name = "Kayak", Price = 275M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
}
};
// get the total value of the products in the cart
decimal cartTotal = cart.TotalPrices();
Console.WriteLine("Total: {0:c}", cartTotal);
Console.ReadKey();
}
}
}
利用this和静态类来实现方法的扩展。this标志了TotalPrices方法所在的类。不好的地方是只能用在一个类中,也就是只能指定一个类。所以我们通过接口避免此弊端。
2、通过接口实现扩展方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
using System.Collections.Generic;
using System.Collections;
public class Product
{
public int ProductID { get; set; }
private string name;
public string Description { get; set;}
public decimal Price { get; set; }
public string Category { set; get;}
public string Name
{
get { return ProductID + name;}
set { name = value; }
}
}
public class ShoppingCart : IEnumerable<Product>
{
public List<Product> Products { get; set; }
public IEnumerator<Product> GetEnumerator()
{
return Products.GetEnumerator();//对Products进行迭代获取Product列表
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
/// <summary>
/// 下面的参数是带this的,类是静态类,怎么扩展一个类的方法
/// </summary>
public static class MyExtensionMethods
{
public static decimal TotalPrices(this IEnumerable<Product> productEnum)
{
decimal total = 0;
foreach (Product prod in productEnum)
{
total += prod.Price;
}
return total;
}
}
class Program
{
static void Main(string[] args)
{
IEnumerable<Product> products = new ShoppingCart
{
Products = new List<Product>
{
new Product {Name = "Kayak", Price = 275M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
}
};
//数组也实现了枚举类型的接口。
Product[] productArray =
{
new Product {Name = "Kayak", Price = 275M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
};
decimal cartTotal = products.TotalPrices();
decimal arrayTotal = productArray.TotalPrices();
Console.WriteLine("Cart Total: {0:c}", cartTotal);
Console.WriteLine("Array Total: {0:c}", arrayTotal);
Console.ReadKey();
}
}
}
数组实现IEnumerable<Product>接口是要注意的地方。除此之外,该方法还能带额外的参数。
3、创造过滤方法
在静态类中添加代码
public static IEnumerable<Product> FilterByCategory(
this IEnumerable<Product> productEnum, string categoryParam)
{
foreach (Product prod in productEnum)
{
if (prod.Category == categoryParam)
{
yield return prod;
}
}
}
在main函数中,添加代码:
IEnumerable<Product> products1 = new ShoppingCart
{
Products = new List<Product> {
new Product {Name = "Kayak", Category = "Watersports", Price = 275M},
new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95M},
new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50M},
new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95M}
}
};
foreach (Product prod in products1.FilterByCategory("Soccer"))
{
Console.WriteLine("Name: {0}, Price {1:c}", prod.Name, prod.Price);
}
4.lamda表达式