且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】

更新时间:2022-09-14 16:59:52

本次笔记分为两个部分:C#语言的部分特性和MVC3里面提供的新视图引擎Razor的@句法。今天晚上的笔记是第一部分:C#的部分特性(我主要选择扩展方法,泛型委托,Lambda表达式等内容,选择这些是因为我对这几个部分不是很熟悉)。如果你已经对C#2.0及3.0里面出现的几个特性比较了解,那你完全可以忽略这部分直接去看下次的关于Razor的笔记。

一,扩展方法(Extension Methods)

1.扩展方法给我们提供了一种很便捷的方式,通过这种方式我可以给那些不是我们自己创建的类(如第三方组件里面的)或是我们不能直接修改的类添加方法。下面是一个关于购物车的例子,我们定义一个ShoppingCart类,如下所示:

《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】
public class ShoppingCart 
{
public List<Product> Products { get; set; }
}

public class Product
{
public int ProductID { get; set; }
public string Name { get; set;}
public string Description { get; set;}
public decimal Price { get; set; }
public string Category { set; get;}
}
《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】

这个类简单吧,呵呵。现在我们需要知道购物车里面Products的总价,但是我们不能去直接更改ShoppingCart类,这种情况的确是存在的,比如这是第三方的组件,我们没有源码。这个问题显然符合定义扩展方法的条件,所以我们可以这样做:

《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】
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;
}
}
《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】

this关键字是必须的,用this指示我们要对哪个类添加方法(称为这个类的扩展方法),后面也可以跟其他类型的参数。这里的参数就是ShoppingCart类型。因为我们计算就是购物车的总价。实现的方法也很简单,遍历购物车里面的商品,把价格累加。

注意:扩展方法并不是让我们通过这样一种规则来打破给类定义方法,属性,字段等,所以我们定义类的成员的时候一般都是在类里面定义。

扩展方法只不过让我们可以在必要的时候扩展类的功能,而且仅仅使用的是你能访问的成员来对其进行扩展。下面看看如何使用上面的定义的扩展方法,如下所示:

《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
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}
}
};
decimal cartTotal = cart.TotalPrices();
Console.WriteLine("Total: {0:c}", cartTotal);
}
}
《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】

2.现在我们对ShoppingCart类进行扩展

《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】
using System.Collections;
using System.Collections.Generic;
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();
}
}
《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】

上面我们让ShoppingCart继承了一个支持迭代的泛型接口,获取了所有的Products。

下面我们修改扩展方法,对IEnumerable<Product>进行扩展,如下所示:

《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】
using System.Collections.Generic;
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;
}
}
《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】

接着对修改过的扩展方法应用,如下所示:

《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】
using System;
using System.Collections.Generic;
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);
}
}
《Pro ASP.NET MVC 3 Framework》学习笔记之六【C#部分特性】

到这里我们会发现一个很有意思的地方,我们定义的IEnumerable<Product>类型的products调用TotalPrices()扩展方法,这个毫无疑问。但是我们这里productArray是一个C#数组,这里竟然也能调用TotalPrices扩展方法。嘿嘿,有点奇怪哈,而且关于C#数组实现IEnumerable<T>的方式有点奇怪这点,我们在MSDN上面也查不到相关的文档。有点奇怪但的确可以这样用的,因为这是编译器支持的,为的是能够兼容早期的C#代码编译。

好了,本次的笔记记到这里。我刚学习,笔记里面肯定理解不到位的地方,请路过的朋友多多指导,谢谢!

祝路过的朋友工作顺利!

晚安!

本文转自Rt-张雪飞博客园博客,原文链接http://www.cnblogs.com/mszhangxuefei/archive/2011/12/06/mvcnotes_6.html如需转载请自行联系原作者


张雪飞