Product.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace LanguageFeatures.Models
{
//public class Product
//{
// private int productID;
// private string name;
// private string description;
// private decimal price;
// private string category; // public int ProductID
// {
// get { return productID; }
// set { productID = value; }
// } // public string Name
// {
// get { return name; }
// set { name = value; }
// } // public string Description
// {
// get { return description; }
// set { description = value; }
// } // public decimal Price
// {
// get { return price; }
// set { price = value; }
// } // public string Category
// {
// get { return category; }
// set { category = value; }
// }
//} // 自动属性
//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; }
//} // 结合起来
public class Product
{
private string name;
public int ProductID { get; set; } public string Name
{
get
{
return ProductID + name;
}
set
{
name = value;
}
} public string Description { get; set; }
public decimal Price { get; set; }
public string Category { set; get; }
}
}

ShoppingCart.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace LanguageFeatures.Models
{
public class ShoppingCart
{
public List<Product> Products { get; set; }
}
}

MyExtensionMethods.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace LanguageFeatures.Models
{ 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;
}
}
}

Controller


public ViewResult UseExtension()
{
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();
return View("Result",
(object)String.Format("Total: {0:c}", cartTotal)); //Total: ¥378.40
}

IEnumerable 改造

ShoppingCart.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace LanguageFeatures.Models
{
public class ShoppingCart:IEnumerable<Product>
{
public List<Product> Products { get; set; }
public IEnumerator<Product> GetEnumerator()
{
return Products.GetEnumerator();
} IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
} }

MyExtensionMethods.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace LanguageFeatures.Models
{ 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;
}
}
}

Controller

public ViewResult UseExtensionEnumerable()
{ 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}
}
}; // create and populate an array of Product objects
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}
}; // get the total value of the products in the cart
decimal cartTotal = products.TotalPrices();
decimal arrayTotal = productArray.TotalPrices(); return View("Result",
(object)String.Format("Cart Total: {0}, Array Total: {1}",
cartTotal, arrayTotal));
}

.Net 扩展的使用的更多相关文章

  1. Asp.net Boilerplate之AbpSession扩展

    当前Abp版本1.2,项目类型为MVC5. 以属性的形式扩展AbpSession,并在"记住我"后,下次自动登录也能获取到扩展属性的值,版权归"角落的白板报"所 ...

  2. 恢复SQL Server被误删除的数据(再扩展)

    恢复SQL Server被误删除的数据(再扩展) 大家对本人之前的文章<恢复SQL Server被误删除的数据> 反应非常热烈,但是文章里的存储过程不能实现对备份出来的日志备份里所删数据的 ...

  3. .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法

    .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...

  4. .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类

    .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...

  5. 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)

    前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...

  6. Dapper扩展之~~~Dapper.Contrib

    平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...

  7. ExtJS 4.2 Date组件扩展:添加清除按钮

    ExtJS中除了提供丰富的组件外,我们还可以扩展他的组件. 在这里,我们将在Date日期组件上添加一个[清除]按钮,用于此组件已选中值的清除. 目录 1. Date组件介绍 2. 主要代码说明 3. ...

  8. .NET Core的文件系统[5]:扩展文件系统构建一个简易版“云盘”

    FileProvider构建了一个抽象文件系统,作为它的两个具体实现,PhysicalFileProvider和EmbeddedFileProvider则分别为我们构建了一个物理文件系统和程序集内嵌文 ...

  9. Hawk 6. 编译和扩展开发

    Hawk是开源项目,因此任何人都可以为其贡献代码.作者也非常欢迎使用者能够扩展出更有用的插件. 编译 编译需要Visual Stuido,版本建议使用2015, 2010及以上没有经过测试,但应该可以 ...

  10. 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇

    最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...

随机推荐

  1. Android TabLayout添加自定义分割线并且可以修改分割线高度

    为TabLayout添加分割线,显示的效果如下(红框内部分): 分割线 首先添加个竖线xml名为layout_divider_vertical: LinearLayout linearLayout = ...

  2. 用jquery控制表格奇偶行及活动行颜色

    虽然jquery流行已经很多年了,一直都感觉很难,也没有花时间去学习它,只是偶尔哪天心血来潮了去看一点点,时隔多日又会忘得一干二净.最近用到表格奇偶行不同色,不得不去再看jquery,虽然感觉还是难, ...

  3. c++几种排序算法代码

    #include <iostream> #include <vector> using namespace std; //交换int void swap(int& a, ...

  4. WLAN HAL

      WLAN HAL WLAN 框架具有三个 WLAN HAL 表面,分别由三个不同的 HIDL 软件包表示: 供应商 HAL:Android 专用命令的 HAL 表面.HIDL 文件位于 hardw ...

  5. Hexo Next 调优

    Links对齐 编辑themes/next/source/css/_custom下的custom.styl,添加如下代码即可. .links-of-blogroll-title{ margin-lef ...

  6. 一 Balancer regionCountCost 权重。

    原文如下地址 : http://www.cnblogs.com/cenyuhai/p/3650943.html 看源码很久了,终于开始动手写博客了,为什么是先写负载均衡呢,因为一个室友入职新公司了,然 ...

  7. CsGL着色的三角形

    转自NeHe教程 public override void Draw() { // Here's Where We Do All The Drawing glClear(GL_COLOR_BUFFER ...

  8. Android 连接网络数据库的方式

    以连接MS SQL(sqlserver数据库)的网络数据库为例,从当前搜集的资料来看,一共有两种方式:在Android工程中引入JDBC驱动,直接连接:通过WebService等方法的间接连接. 采用 ...

  9. View 和 ViewGroup 的 hasFocusable

    在 android 中.焦点的获取和事件几乎相同,有一个分发机制.一般来说View 树上上层节点的 ViewGroup 比底层节点的 View 有更高的优先级获取焦点.这体如今 ViewGroup 有 ...

  10. Project Euler:Problem 58 Spiral primes

    Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...