zoukankan      html  css  js  c++  java
  • .Net 扩展的使用

    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));
            }
    
  • 相关阅读:
    Python multiprocessing相关疑问
    Tornado demo3
    WebSockets
    Tornado Demo1---webspider分析
    Python assert断言
    Python学习之--数据基础
    Python学习之--python概要
    Python学习之--函数/生成器/装饰器
    Python学习之文件操作
    Python操作MySQL数据库
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6895470.html
Copyright © 2011-2022 走看看