Đang chuẩn bị liên kết để tải về tài liệu:
ebook Addison -Essential CSharp 4.0_7

Không đóng trình duyệt đến khi xuất hiện nút TẢI XUỐNG

Tham khảo tài liệu 'ebook addison -essential csharp 4.0_7', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Iterators 639 CSharpPrimitiveTypes primitives new CSharpPrimitiveTypes foreach string primitive in primitives Console.WriteLine primitive The results of Listing 16.13 appear in Output 16.5. Output 16.5 object byte uint ulong float char bool ushort decimal int sbyte short long void double string The output from this listing is a listing of the C primitive types.1 Iterators and State When an iterator is first called in a foreach statement such as foreach string primitive in primitives in Listing 16.13 its state is initialized within the enumerator. The iterator maintains its state as long as the foreach statement at the call site continues to execute. When you yield a value process it and resume the foreach statement at the call site the iterator continues where it left off the previous time around the loop and 1. In alpha versions of the C 2.0 compiler yield was a keyword rather than a contextual keyword. However such a change could result in an incompatibility between C 1.0 and C 2.0. Instead yield became a contextual keyword that must appear before return. As a result no code-breaking change occurred because C 1.0 did not allow any text besides comments prior to the return keyword. Chapter 16 Building Custom Collections continues processing. When the foreach statement at the call site terminates the iterator s state is no longer saved. It is always safe to call the iterator again since the generated code never resets the state of the iterator but instead creates a new one when needed. Figure 16.8 shows a high-level sequence diagram of what takes place. Remember that the MoveNext method appears on the IEnumerator T interface. MoveNext yield return byte WriteLine MoveNext yield return string WriteLine Figure 16.8 Sequence Diagram with yield return Iterators 641 In Listing 16.13 the foreach statement at the call site initiates a call to GetEnumerator on the CSharpPrimitiveTypes instance called primitives. Given the iterator instance referenced by iterator foreach .

Đã 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.