The extension method is used in MVC, And the extension method is used in mvc.

Source: Internet
Author: User

The extension method is used in MVC, And the extension method is used in mvc.

Extension Method is a convenient way to add methods to classes that are not owned by you and cannot be directly modified.

1. Use extension methods

1. Define a shopping cart class-ShoppingCart

 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6  7 namespace Demo.Models 8 { 9     public class ShoppingCart:IEnumerable<Product>10     {11         public List<Product> Products { get; set; }16     }17 }

2. Define an Extension Method

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5  6 namespace Demo.Models 7 { 8     public static class MyExtensionMethods 9     {10         public static decimal TotalPrices(this ShoppingCart cartParam)11         {12             decimal total = 0;13             foreach (Product prod in cartParam.Products)14             {15                 total += prod.Price;16             }17             return total;18         }28     }29 }

This keyword defines TotalPrices as an extension method ShoppingCart. Net extension method and that class

3. Use extension methods

 

1 public ViewResult UserExtension () 2 {3 // create and fill ShoppingCart 4 ShoppingCart cart = new ShoppingCart 5 {6 Products = new List <Product> {7 new Product {Name = "kayak ", price = 275 M}, // kayak 8 new Product {Name = "Lifejacket", Price = 48.95 M}, // casual jacket 9 new Product {Name = "Soccer ball ", price = 19.50 M}, // football 10 new Product {Name = "Corner flag", Price = 34.95 M} // Corner flag 11} 12 }; 13 // calculate the total product price in the cart. 14 decimal cartTotal = cart. totalPrices (); 15 return View ("Result", (object) String. format ("Total: {0: c}", cartTotal); 16}

 

4. Result Display

Ii. Extend the interface

1. implement interfaces in the ShoppingCart class

 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6  7 namespace Demo.Models 8 { 9     public class ShoppingCart:IEnumerable<Product>10     {11         public List<Product> Products { get; set; }12         public IEnumerator<Product> GetEnumerator()13 {14 return Products.GetEnumerator();15 }16  IEnumerator IEnumerable.GetEnumerator()17 {18 return GetEnumerator();19 }20     }21 }

2. Expansion Method on the Interface

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5  6 namespace Demo.Models 7 { 8     public static class MyExtensionMethods 9     {10         public static decimal TotalPrices(this IEnumerable<Product> productEnum)11         { 12             decimal total=0;13             foreach(Product prod in productEnum)14             {15                 total += prod.Price;16             }17             return total;18         }19     }20 }

3. Apply the extension method to different implementations of the same interface

1 public ViewResult UseExtensionEnumerable () 2 {3 IEnumerable <Product> products = new ShoppingCart 4 {5 Products = new List <Product> {6 new Product {Name = "kayak ", price = 275 M}, // kayak 7 new Product {Name = "Lifejacket", Price = 48.95 M}, // casual jacket 8 new Product {Name = "Soccer ball ", price = 19.50 M}, // football 9 new Product {Name = "Corner flag", Price = 34.95 M} // Corner flag 10} 11 }; 12 Product [] productArary = {13 new Product {Name = "kayak", Price = 375 M}, // kayak 14 new Product {Name = "Lifejacket ", price = 48.95 M}, // casual jacket 15 new Product {Name = "Soccer ball", Price = 19.50 M }, // football 16 new Product {Name = "Corner flag", Price = 34.95 M} // Corner flag 17}; 18 // get the total Price of products in the shopping cart 19 decimal cartTotal = products. totalPrices (); 20 // get the total price of the product in the array 21 decimal arrayTotal = productArary. totalPrices (); 22 return View ("Result", (object) String. format ("Cart Total: {0}, Array Total: {1}", cartTotal, arrayTotal); 23}

4. Result Display

3. Create a filtering Extension Method

1,

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5  6 namespace Demo.Models 7 { 8     public static class MyExtensionMethods 9     {10         public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product> productEnum, string categoryParm)11         {12             foreach (Product prod in productEnum)13             {14                 if (prod.Category == categoryParm)15                 {16                     yield return prod;17                 }18             }19         }20     }21 }

2. Use the Filter Extension Method

1 public ViewResult UseFilterExtensionMethod () 2 {3 IEnumerable <Product> products = new ShoppingCart 4 {5 Products = new List <Product> {6 new Product {Name = "kayak ", category = "Watersports", Price = 375 M}, // kayak 7 new Product {Name = "Lifejacket", Category = "Watersports", Price = 48.95 M }, // casual jacket 8 new Product {Name = "Soccer ball", Category = "Soccer", Price = 19.50 M }, // football 9 new Product {Name = "Corner flag", Category = "Soccer", Price = 34.95 M} // Corner flag 10} 11}; 12 decimal total = 0; 13 foreach (Product prod in products. filterByCategory ("Soccer") 14 {15 total + = prod. price; 16} 17 return View ("Result", (object) String. format ("Total: {0}", total); 18}

3. Result Display

Only the price in the Soccer category is returned and accumulated.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.