tailieunhanh - Apress Introducing Dot Net 4 With Visual Studio_8
Tham khảo tài liệu 'apress introducing dot net 4 with visual studio_8', công nghệ thông tin, hệ điều hành phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | CHAPTER 14 EXTENSION METHODS changed. C on the other hand offers a hybrid environment in which you are free to implement functional programming if you choose. Also those familiar with the Standard Template Library STL will get a familiar feeling from this style of programming. STL swept through the C programming community back in the early 1990s and encouraged a more functional programming thought process. Operation Chaining Using extension methods operation chaining becomes a more natural process. Again it s nothing that you could not have done in the C days using plain static methods and anonymous methods. However with the streamlined syntax chaining actually removes the clutter and can trigger some innovative thinking. Let s start with the example from the previous section in which we took a list of integers and transformed them into a list of doubles. This time we ll look at how we can actually chain operations in a fluid way. Let s suppose that after dividing the integers by 3 we want to then compute the square of the result. The following code shows how to do that using System using using public static class MyExtensions public static IEnumerable R Transform T R this IEnumerable T input Func T R op foreach var item in input yield return op item public class TransformExample static IEnumerable int CreateInfiniteList int n 0 while true yield return n static double DivideByThree int n return double n 3 static double Square double r return r r static void Main var divideByThree new Func int double DivideByThree var squareNumber 502 CHAPTER 14 EXENTENSION METHODS new Func double double Square var result CreateInfiniteList . Transform divideByThree . Transform squareNumber var iter for int i 0 i 25 i Isn t that cool In one statement of code I took an infinite list of integers and applied a divisor followed by a squaring operation and the end result is a .
đang nạp các trang xem trước