• .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+SparkStreaming+kafka+写入本地文件案例(可执行)
    Python安装pycurl失败,及解决办法
    Linux screen用法简介
    [算法]数组中求出下标不连续的任意个数,使得和最大
    消息队列小结
    [算法]计算全排列组合数
    [数据结构]A*寻路算法
    [数据结构]最大流之Ford-Fulkerson算法
    [数据结构]最小生成树算法Prim和Kruskal算法
    [数据结构]迪杰斯特拉(Dijkstra)算法
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6895470.html
Copyright © 2020-2023  润新知