TAILIEUCHUNG - C# in Depth what you need to master c2 and 3 phần 4

chúng tôi chỉ có thể sử dụng vô giá trị để đại diện cho giá không rõ, nhưng vì nó là một loại giá trị, chúng ta có thể không. Làm thế nào bạn sẽ đại diện cho điều này trong C # 1? Có ba lựa chọn thay thế phổ biến: | Generic collection classes in .NET 97 Remove all elements in the list matching a given predicate RemoveAll . Perform a given action on each element on the list ForEach .10 We ve already seen the ConvertAll method in listing but there are two more delegate types that are very important for this extra functionality Predicate T and Action T which have the following signatures public delegate bool Predicate T T obj public delegate void Action T T obj A predicate is a way of testing whether a value matches a criterion. For instance you could have a predicate that tested for strings having a length greater than 5 or one that tested whether an integer was even. An action does exactly what you might expect it to performs an action with the specified value. You might print the value to the console add it to another collection whatever you want. For simple examples most of the methods listed here are easily achieved with a foreach loop. However using a delegate allows the behavior to come from somewhere other than the immediate code in the foreach loop. With the improvements to delegates in C 2 it can also be a bit simpler than the loop. Listing shows the last two methods ForEach and RemoveAll in action. We take a list of the integers from 2 to 100 remove multiples of 2 then multiples of 3 and so forth up to 10 finally listing the numbers. You may well recognize this as a slight variation on the Sieve of Eratosthenes method of finding prime numbers. I ve used the streamlined method of creating delegates to make the example more realistic. Even though we haven t covered the syntax yet you can peep ahead to chapter 5 if you want to get the details it should be fairly obvious what s going on here. Listing Printing primes using RemoveAll and ForEach from List T List int candidates new List int for int i 2 i 100 i i for int factor 2 factor 10 factor delegate int x return x factor x factor 0 delegate int .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.