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. POJ 3273 二分答案

    思路:二分答案经典题吧....注意边界就OK了 //By SiriusRen #include <cstdio> #include <algorithm> using name ...

  2. [C++] upper_bound和lower_bound

    upper_bound 源码 template <class ForwardIterator, class T> ForwardIterator upper_bound (ForwardI ...

  3. 使用regasm注册.net com组件出现不是有效的.net程序集的解决办法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在电脑上装有VS 2008和VS 2010.使用VS 2010编写了一个C# com组件:MyCom(基于.net f ...

  4. iOS-RAC学习笔记(一)——RACStream

    RACStream是RACSignal和RACSequence的父类,定义了一些流的操作方法.从名字上可以看出来,这个对象就像流一样可以往任何一个出口流,同时也可以给这个流设计一道道关卡,改变流(这里 ...

  5. vue中使用console.log无效

    webpack开发环境下,在vue中使用console.log无效,一直以为webpack出了问题. 使用window.console.log()就能够顺利在浏览器控制台输出了. 以及 在axios请 ...

  6. webStrom的破解以及汉化

    破解方法: 把JetbrainsCrack-3.1-release-enc.jar包放到bin目录下,然后把webstorm64.exe.vmoptions文件用文本打开, 在最后面加上一句-java ...

  7. numpy基础篇-简单入门教程3

    np import numpy as np np.__version__ print(np.__version__) # 1.15.2 numpy.arange(start, stop, step, ...

  8. 【Henu ACM Round#19 F】Dispute

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 这一题和这一题很像 (链接 ) 会发现如果a[i]!=b[i]那么就按下i就好了. 然后改变和他相邻的点. 此后a[i]再也不可能和 ...

  9. Android——4.2 - 3G移植之路之 AT 通信 (四)

    在前文Android--4.2 - 3G移植之路之 reference-ril .pppd 拨号上网 (三)中分析了3G连接网络的流程,当中有说道通过AT指令建立连接, 在这里记录一下3G中的AT通信 ...

  10. h5 图片回显

    <form method="post" id="imgForm" action ="/hi/holdHead" enctype=&qu ...