Pro ASP. net mvc 3 Framework study Note 6

Source: Internet
Author: User

 

 

This note is divided into two parts: the features of the C # language and the @ syntax of the new view engine Razor provided in MVC3. This evening's note is Part 1: some features of c # (I mainly choose extension methods, generic delegation, Lambda expressions, etc, this is because I am not very familiar with these parts ). If you already know more about the features in C #2.0 and 3.0, you can ignore this part and directly go to the next Razor note.

 

1. Extension Methods)

1. the extension method provides us with a very convenient way. In this way, I can give classes that are not created by ourselves (such as those in third-party components) or the class addition method that we cannot directly modify. The following is an example of Shopping Cart. We define a ShoppingCart class, as shown below:

 

Public class ShoppingCart

{

Public List <Product> Products {get; set ;}

}

 

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 ;}

}

 

This class is simple. Now we need to know the total price of Products in the shopping cart, but we cannot directly change the ShoppingCart class. This does exist. For example, this is a third-party component and we do not have the source code. This problem clearly meets the criteria for defining the extension method, so we can do this:

 

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;

}

}

 

The this keyword is required. Use this to indicate which class we want to add (called the extension method of this class). It can be followed by other types of parameters. The parameter here is the ShoppingCart type. Because our calculation is the total price of the shopping cart. The implementation method is also very simple. traverse the items in the shopping cart and accumulate the price.

Note: The extension method does not allow us to use such a rule to break the class definition methods, attributes, fields, etc. Therefore, when we define class members, they are generally defined in the class. The extension method only allows us to extend the functions of the class when necessary, and only uses the members you can access to expand the class. The following describes how to use the extension method defined above:

 

Using System;

Using System. Collections. Generic;

Class Program

{

Static void Main (string [] args)

{

ShoppingCart cart = new ShoppingCart

{

Products = new List <Product>

{

New Product {Name = "Kayak", Price = 275 M },

New Product {Name = "Lifejacket", Price = 48.95 M },

New Product {Name = "Soccer ball", Price = 19.50 M },

New Product {Name = "Corner flag", Price = 34.95 M}

}

};

Decimal cartTotal = cart. TotalPrices ();

Console. WriteLine ("Total: {0: c}", cartTotal );

}

}

 

2. Now we can extend the ShoppingCart class.

 

Using System. Collections;

Using System. Collections. Generic;

Public class ShoppingCart: IEnumerable <Product>

{

Public List <Product> Products {get; set ;}

Public IEnumerator <Product> GetEnumerator ()

{

Return Products. GetEnumerator (); // iteratively retrieves the Product list for Products.

}

IEnumerator IEnumerable. GetEnumerator ()

{

Return GetEnumerator ();

}

}

 

In the above example, ShoppingCart inherits a generic interface that supports iteration and obtains all Products.

Next we modify the extension method to expand IEnumerable <Product>, as shown below: www.2cto.com

 

Using System. Collections. Generic;

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;

}

}

 

Then, apply the modified extension method as follows:

 

Using System;

Using System. Collections. Generic;

Class Program

{

Static void Main (string [] args)

{

IEnumerable <Product> products = new ShoppingCart

{

Products = new List <Product>

{

New Product {Name = "Kayak", Price = 275 M },

New Product {Name = "Lifejacket", Price = 48.95 M },

New Product {Name = "Soccer ball", Price = 19.50 M },

New Product {Name = "Corner flag", Price = 34.95 M}

}

};

Product [] productArray =

{

New Product {Name = "Kayak", Price = 275 M },

New Product {Name = "Lifejacket", Price = 48.95 M },

New Product {Name = "Soccer ball", Price = 19.50 M },

New Product {Name = "Corner flag", Price = 34.95 M}

};

Decimal cartTotal = products. TotalPrices ();

Decimal arrayTotal = productArray. TotalPrices ();

Console. WriteLine ("Cart Total: {0: c}", cartTotal );

Console. WriteLine ("Array Total: {0: c}", arrayTotal );

}

}

 

Here we will find a very interesting place. We have no doubt that the defined IEnumerable <Product> type products calls the TotalPrices () Extension Method. However, productArray is a C # array, and the TotalPrices extension method can also be called here. Hey, it's a bit strange, and it's a bit strange about how the C # array implements IEnumerable <T>. We can't find the relevant documentation on MSDN. It's a bit strange, but it can be used like this, because this is supported by the compiler, in order to be compatible with early C # code compilation.

 

Okay. Here is the note. I just learned that I cannot understand the place in my notes. Please give me more guidance. Thank you!

 

Wish the passing friends a smooth job!

 

Good night!


 

Author Gabriel Zhang

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.