Visual C# 2010 Recipes solution_6
Số trang: 95
Loại file: pdf
Dung lượng: 1.89 MB
Lượt xem: 11
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 visual c# 2010 recipes solution_6, 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:
Visual C# 2010 Recipes solution_6 CHAPTER 13 ■ COMMONLY USED INTERFACES AND PATTERNS 13-5. Implement an Enumerable Type Using a Custom Iterator Problem You need to create an enumerable type but do not want to rely on the built-in iterator support provided by the .NET Framework (described in recipe 13-4). Solution Implement the interface System.Collections.IEnumerable on your collection type. The GetEnumerator method of the IEnumerable interface returns an enumerator, which is an object that implements the interface System.Collections.IEnumerator. The IEnumerator interface defines the methods used by the foreach statement to enumerate the collection. Implement a private inner class within the enumerable type that implements the interface IEnumerator and can iterate over the enumerable type while maintaining appropriate state information. In the GetEnumerator method of the enumerable type, create and return an instance of the iterator class. How It Works The automatic iterator support built into C# is very powerful and will be sufficient in the majority of cases. However, in some cases you may want to take direct control of the implementation of your collection’s iterators. For example, you may want an iterator that supports changes to the underlying collection during enumeration. Whatever your reason, the basic model of an enumerable collection is the same as that described in recipe 13-4. Your enumerable type should implement the IEnumerable interface, which requires you to implement a method named GetEnumerator. However, instead of using the yield return statement in GetEnumerator, you must instantiate and return an object that implements the IEnumerator interface. The IEnumerator interface provides a read-only, forward-only cursor for accessing the members of the underlying collection. Table 13-2 describes the members of the IEnumerator interface. The IEnumerator instance returned by GetEnumerator is your custom iterator—the object that actually supports enumeration of the collection’s data elements.640 CHAPTER 13 ■ COMMONLY USED INTERFACES AND PATTERNSTable 13-2. Members of the IEnumerator InterfaceMember DescriptionCurrent Property that returns the current data element. When the enumerator is created, Current refers to a position preceding the first data element. This means you must call MoveNext before using Current. If Current is called and the enumerator is positioned before the first element or after the last element in the data collection, Current must throw a System.InvalidOperationException.MoveNext Method that moves the enumerator to the next data element in the collection. Returns true if there are more elements; otherwise, it returns false. If the underlying source of data changes during the life of the enumerator, MoveNext must throw an InvalidOperationException.Reset Method that moves the enumerator to a position preceding the first element in the data collection. If the underlying source of data changes during the life of the enumerator, Reset must throw an InvalidOperationException. If your collection class contains different types of data that you want to enumerate separately,implementing the IEnumerable interface on the collection class is insufficient. In this case, you wouldimplement a number of properties that return different IEnumerator instances.The CodeThe TeamMember, Team, and TeamMemberEnumerator classes in the following example demonstrate theimplementation of a custom iterator using the IEnumerable and IEnumerator interfaces. The TeamMemberclass represents a member of a team. The Team class, which represents a team of people, is a collection ofTeamMember objects. Team implements the IEnumerable interface and declares a separate class, namedTeamMemberEnumerator, to provide enumeration functionality. Team implements the Observer patternusing delegate and event members to notify all TeamMemberEnumerator objects if their underlying Teamchanges. (See recipe 13-11 for a detailed description of the Observer pattern.) The TeamMemberEnumeratorclass is a private nested class, so you cannot create instances of it other than through theTeam.GetEnumerator method.using System;using System.Collections;namespace Apress.VisualCSharpRecipes.Chapter13{ // TeamMember class represents an individual team member. public class TeamMember { public string Name; public string Title; 641 CHAPTER 13 ■ COMMONLY USED INTERFACES AND PATTERNS ...
Nội dung trích xuất từ tài liệu:
Visual C# 2010 Recipes solution_6 CHAPTER 13 ■ COMMONLY USED INTERFACES AND PATTERNS 13-5. Implement an Enumerable Type Using a Custom Iterator Problem You need to create an enumerable type but do not want to rely on the built-in iterator support provided by the .NET Framework (described in recipe 13-4). Solution Implement the interface System.Collections.IEnumerable on your collection type. The GetEnumerator method of the IEnumerable interface returns an enumerator, which is an object that implements the interface System.Collections.IEnumerator. The IEnumerator interface defines the methods used by the foreach statement to enumerate the collection. Implement a private inner class within the enumerable type that implements the interface IEnumerator and can iterate over the enumerable type while maintaining appropriate state information. In the GetEnumerator method of the enumerable type, create and return an instance of the iterator class. How It Works The automatic iterator support built into C# is very powerful and will be sufficient in the majority of cases. However, in some cases you may want to take direct control of the implementation of your collection’s iterators. For example, you may want an iterator that supports changes to the underlying collection during enumeration. Whatever your reason, the basic model of an enumerable collection is the same as that described in recipe 13-4. Your enumerable type should implement the IEnumerable interface, which requires you to implement a method named GetEnumerator. However, instead of using the yield return statement in GetEnumerator, you must instantiate and return an object that implements the IEnumerator interface. The IEnumerator interface provides a read-only, forward-only cursor for accessing the members of the underlying collection. Table 13-2 describes the members of the IEnumerator interface. The IEnumerator instance returned by GetEnumerator is your custom iterator—the object that actually supports enumeration of the collection’s data elements.640 CHAPTER 13 ■ COMMONLY USED INTERFACES AND PATTERNSTable 13-2. Members of the IEnumerator InterfaceMember DescriptionCurrent Property that returns the current data element. When the enumerator is created, Current refers to a position preceding the first data element. This means you must call MoveNext before using Current. If Current is called and the enumerator is positioned before the first element or after the last element in the data collection, Current must throw a System.InvalidOperationException.MoveNext Method that moves the enumerator to the next data element in the collection. Returns true if there are more elements; otherwise, it returns false. If the underlying source of data changes during the life of the enumerator, MoveNext must throw an InvalidOperationException.Reset Method that moves the enumerator to a position preceding the first element in the data collection. If the underlying source of data changes during the life of the enumerator, Reset must throw an InvalidOperationException. If your collection class contains different types of data that you want to enumerate separately,implementing the IEnumerable interface on the collection class is insufficient. In this case, you wouldimplement a number of properties that return different IEnumerator instances.The CodeThe TeamMember, Team, and TeamMemberEnumerator classes in the following example demonstrate theimplementation of a custom iterator using the IEnumerable and IEnumerator interfaces. The TeamMemberclass represents a member of a team. The Team class, which represents a team of people, is a collection ofTeamMember objects. Team implements the IEnumerable interface and declares a separate class, namedTeamMemberEnumerator, to provide enumeration functionality. Team implements the Observer patternusing delegate and event members to notify all TeamMemberEnumerator objects if their underlying Teamchanges. (See recipe 13-11 for a detailed description of the Observer pattern.) The TeamMemberEnumeratorclass is a private nested class, so you cannot create instances of it other than through theTeam.GetEnumerator method.using System;using System.Collections;namespace Apress.VisualCSharpRecipes.Chapter13{ // TeamMember class represents an individual team member. public class TeamMember { public string Name; public string Title; 641 CHAPTER 13 ■ COMMONLY USED INTERFACES AND PATTERNS ...
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 317 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 290 0 0 -
70 trang 251 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 238 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 233 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 214 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 208 0 0 -
Tổng hợp 30 lỗi thương gặp cho những bạn mới sử dụng máy tính
9 trang 205 0 0 -
UltraISO chương trình ghi đĩa, tạo ổ đĩa ảo nhỏ gọn
10 trang 204 0 0