TAILIEUCHUNG - Enumerating Variables Part

Enumerating the Elements in Collection In Chapter 10, you saw an example of using the foreach statement to list the items in a simple array. The code looked like this: int[] pins = { 9, 3, 7, 2 }; foreach (int pin in pins) | Enumerating the Elements in Collection In Chapter 10 you saw an example of using the foreach statement to list the items in a simple array. The code looked like this int pins 9 3 7 2 foreach int pin in pins pin The foreach construct provides an elegant mechanism that greatly simplifies the code that you need to write but it can only be exercised under certain circumstances you can only use foreach to step through an enumerable collection. So what exactly is an enumerable collection The quick answer is that it is a collection that implements the interface. NOTE Remember that all arrays in C are actually instances of the class. The class is a collection class that implements the lEnumerable interface. The lEnumerable interface contains a single method called GetEnumerator lEnumerator GetEnumerator The GetEnumerator method returns an enumerator object that implements the interface. The enumerator object is used for stepping through enumerating the elements of the collection. The lEnumerator interface itself specifies the following property and methods object Current get bool MoveNext void Reset Think of an enumerator as a pointer pointing to elements in a list. Initially the pointer points before the first element. You call the MoveNext method to move the pointer down to the next first item in the list the MoveNext method should return true if there actually is another item and false if there isn t. You use the Current property to access the item currently pointed to and the Reset method to return the pointer back to before the first item in the list. By creating an enumerator by using the GetEnumerator method of a collection and repeatedly calling the MoveNext method and retrieving the value of the Current property by using the enumerator you can move forward through the elements of a collection one item at a time. This is exactly what the foreach statement does.

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.