Addison essential C# 4.0 Visual Studio_8
Số trang: 98
Loại file: pdf
Dung lượng: 1.88 MB
Lượt xem: 14
Lượt tải: 0
Xem trước 10 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu addison essential c# 4.0 visual studio_8, 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ả
Nội dung trích xuất từ tài liệu:
Addison essential C# 4.0 Visual Studio_8 639 I terators 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 stringThe output from this listing is a listing of the C# primitive types.1Iterators and StateWhen an iterator is first called in a foreach statement (such as foreach(string primitive in primitives) in Listing 16.13), its state is initializedwithin the enumerator. The iterator maintains its state as long as theforeach statement at the call site continues to execute. When you yield avalue, process it, and resume the foreach statement at the call site, the iter-ator continues where it left off the previous time around the loop and1. 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. From the Library of Wow! eBook640 C hapter 16: Building Custom Collections continues processing. When the foreach statement at the call site termi- nates, the iterator’s state is no longer saved. It is always safe to call the iter- ator 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 interface. primitives: enumerator: Program Console CSharpPrimitiveTypes Enumerator GetEnumerator() Instantiate MoveNext() yield return object WriteLine() MoveNext() yield return byte WriteLine() ... MoveNext() yield return string WriteLine() Figure 16.8: Sequence Diagram with yield return From the Library of Wow! eBook 641 I terators In Listing 16.13, the foreach statement at the call site initiates a call toGetEnumerator() on the CSharpPrimitiveTypes instance called primi-tives. Given the iterator instance (referenced by iterator), foreach beginseach iteration with a call to MoveNext(). Within the iterator, you yield avalue back to the foreach statement at the call site. After the yield returnstatement, the GetEnumerator() method seemingly pauses until the nextMoveNext() request. Back at the call site, the foreach statement displays theyielded value on the screen. It then loops back around and calls MoveNext()on the iterator again. Notice that the second time, processing picks up at thesecond yield return statement. Once again, the foreach displays on thescreen what CSharpPrimitiveTypes yielded and starts the loop again. Thisprocess continues until there are no more yield return statements withinthe iterator. At that point, the foreach loop at the call site terminates.More Iterator ExamplesBefore you modify BinaryTree, you must modify Pair to supportthe IEnumerable interface using an iterator. Listing 16.14 is an examplethat yields each element in Pair.Listing 16.14: Using yield to Implement BinaryTree public struct Pair: IPair, IEnumerable { public Pair(T first, T second) { _first = first; _second = second; } public T First { get{ return _first; } private set{ _first = value; } } private T _first; public T Second { get{ return _second; } ...
Nội dung trích xuất từ tài liệu:
Addison essential C# 4.0 Visual Studio_8 639 I terators 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 stringThe output from this listing is a listing of the C# primitive types.1Iterators and StateWhen an iterator is first called in a foreach statement (such as foreach(string primitive in primitives) in Listing 16.13), its state is initializedwithin the enumerator. The iterator maintains its state as long as theforeach statement at the call site continues to execute. When you yield avalue, process it, and resume the foreach statement at the call site, the iter-ator continues where it left off the previous time around the loop and1. 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. From the Library of Wow! eBook640 C hapter 16: Building Custom Collections continues processing. When the foreach statement at the call site termi- nates, the iterator’s state is no longer saved. It is always safe to call the iter- ator 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 interface. primitives: enumerator: Program Console CSharpPrimitiveTypes Enumerator GetEnumerator() Instantiate MoveNext() yield return object WriteLine() MoveNext() yield return byte WriteLine() ... MoveNext() yield return string WriteLine() Figure 16.8: Sequence Diagram with yield return From the Library of Wow! eBook 641 I terators In Listing 16.13, the foreach statement at the call site initiates a call toGetEnumerator() on the CSharpPrimitiveTypes instance called primi-tives. Given the iterator instance (referenced by iterator), foreach beginseach iteration with a call to MoveNext(). Within the iterator, you yield avalue back to the foreach statement at the call site. After the yield returnstatement, the GetEnumerator() method seemingly pauses until the nextMoveNext() request. Back at the call site, the foreach statement displays theyielded value on the screen. It then loops back around and calls MoveNext()on the iterator again. Notice that the second time, processing picks up at thesecond yield return statement. Once again, the foreach displays on thescreen what CSharpPrimitiveTypes yielded and starts the loop again. Thisprocess continues until there are no more yield return statements withinthe iterator. At that point, the foreach loop at the call site terminates.More Iterator ExamplesBefore you modify BinaryTree, you must modify Pair to supportthe IEnumerable interface using an iterator. Listing 16.14 is an examplethat yields each element in Pair.Listing 16.14: Using yield to Implement BinaryTree public struct Pair: IPair, IEnumerable { public Pair(T first, T second) { _first = first; _second = second; } public T First { get{ return _first; } private set{ _first = value; } } private T _first; public T Second { get{ return _second; } ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhGợi ý tài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 315 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 305 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 288 0 0 -
70 trang 250 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 237 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 232 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 213 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 207 0 0 -
UltraISO chương trình ghi đĩa, tạo ổ đĩa ảo nhỏ gọn
10 trang 203 0 0 -
Phần III: Xử lý sự cố Màn hình xanh
3 trang 203 0 0