tailieunhanh - ASP.NET 4 Unleased - p 98

Điều đẹp về generics ở đây là bạn không phải viết cùng một mã để chuyển đổi một đầu đọc dữ liệu vào một danh sách chung cho mỗi loại. Bạn viết GetListFromCommand () phương pháp như là một phương pháp chung một lần, và bạn có thể sử dụng phương pháp với bất kỳ loại đáp ứng các ràng buộc chung trong tương lai. | 944 CHAPTER 20 Data Access with LINQ to SQL The beautiful thing about generics here is that you don t have to write the same code to convert a data reader to a generic List for each type. You write the GetListFromCommand method as a generic method once and you can use the method with any type that meets the generic constraints in the future. The right way to think of generics is to think of a code template. You can use generics to define a certain pattern of code into which you can plug a particular type. Understanding Lambda Expressions Lambda expressions another language feature introduced with .NET Framework provide you with an extremely terse way of defining methods. Imagine for example that you want to programmatically wire up a Click event handler to a button control. Listing is an example of one way of doing this. LISTING LanguageChanges @ Page Language C DOCTYPE html PUBLIC - W3C DTD XHTML Transitional EN http TR xhtml1 DTD script runat server void Page_Init new EventHandler btn_Click void btn_Click object sender EventArgs e script html xmlns http 1999 xhtml head runat server title Normal Method title head body form id form1 runat server div asp Button id btn Text Go Runat server From the Library of Wow eBook New C and Language Features 945 asp Label id lblResult Runat server div form body html In Listing the Page_Init method associates the Button Click event with the btn_Click method. When you click the button the btn_Click method executes and displays the current date and time. Nothing special here. In .NET Framework the notion of anonymous methods for C was introduced. The advantage of an anonymous method is that you can declare it inline. For example Listing does the same thing as the previous page except for that it uses an anonymous method to handle the Button Click event. LISTING LanguageChanges .