Study Notes in pro ASP. net mvc 3 framework [C # features]

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 = 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();
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:

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 = 275M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
}
};
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}
};
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!

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.