![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Addison essential C# 4.0 Visual Studio_6
Số trang: 98
Loại file: pdf
Dung lượng: 1.85 MB
Lượt xem: 15
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_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:
Addison essential C# 4.0 Visual Studio_6 443 C onstraintsimplement the IComparable interface. The syntax for this appears inListing 11.22.Listing 11.22: Declaring an Interface Constraint public class BinaryTree where T: System.IComparable { ... public Pair SubItems { get{ return _SubItems; } set { IComparable first; // Notice that the cast can now be eliminated. first = value.First.Item; if (first.CompareTo(value.Second.Item) < 0) { // first is less than second ... } else { // second is less than or equal to first. ... } _SubItems = value; } } private Pair _SubItems; } Given the interface constraint addition in Listing 11.22, the compilerensures that each time you use the BinaryTree class you specify a typeparameter that implements the IComparable interface. Furthermore,you no longer need to explicitly cast the variable to an IComparableinterface before calling the CompareTo() method. Casting is not evenrequired to access members that use explicit interface implementation,which in other contexts would hide the member without a cast. To resolvewhat member to call, the compiler first checks class members directly, andthen looks at the explicit interface members. If no constraint resolves theargument, only members of object are allowable. From the Library of Wow! eBook444 C hapter 11: Generics If you tried to create a BinaryTree variable using System. Text.StringBuilder as the type parameter, you would receive a compiler error because StringBuilder does not implement IComparable. The error is similar to the one shown in Output 11.3. OUTPUT 11.3: error CS0309: The type ’System.Text.StringBuilder’ must be convertible to ’System.IComparable’ in order to use it as parameter ’T’ in the generic type or method ’BinaryTree’ To specify an interface for the constraint you declare an interface con- straint. This constraint even circumvents the need to cast in order to call an explicit interface member implementation. Base Class Constraints Sometimes you might want to limit the constructed type to a particular class derivation. You do this using a base class constraint, as shown in Listing 11.23. Listing 11.23: Declaring a Base Class Constraint public class EntityDictionary : System.Collections.Generic.Dictionary where TValue : EntityBase { ... } In contrast to System.Collections.Generic.Dictionary on its own, EntityDictionary requires that all TValue types derive from the EntityBase class. By requiring the derivation, it is possible to always perform a cast operation within the generic implemen- tation, because the constraint will ensure that all type parameters derive from the base and, therefore, that all TValue type parameters used with EntityDictionary can be implicitly converted to the base. The syntax for the base class constraint is the same as that for the inter - face constraint, except that base class constraints must appear first when multiple constraints are specified. However, unlike interface constraints, From the Library of Wow! eBook 445 C onstraintsmultiple base class constraints are not allowed since it is not possible toderive from multiple classes. Similarly, base class constraints cannot bespecified for sealed classes or specific structs. For example, C# does notallow a constraint for a type parameter to be derived from string or Sys-tem.Nullable.struct/class ConstraintsAnother valuable generic constraint is the ability to restrict type parame-ters to a value type or a reference type. The compiler does not allow speci-fying System.ValueType as the base class in a constraint. Instead, C#provides special syntax that works for reference types as well. Instead ofspecifying a class from which T must derive, you simply use the keywordstruct or class, as shown in Listing 11.24.Listing 11.24: Specifying the Type Parameter As a Value Type public struct Nullable : IFormattable, IComparable, ICompa ...
Nội dung trích xuất từ tài liệu:
Addison essential C# 4.0 Visual Studio_6 443 C onstraintsimplement the IComparable interface. The syntax for this appears inListing 11.22.Listing 11.22: Declaring an Interface Constraint public class BinaryTree where T: System.IComparable { ... public Pair SubItems { get{ return _SubItems; } set { IComparable first; // Notice that the cast can now be eliminated. first = value.First.Item; if (first.CompareTo(value.Second.Item) < 0) { // first is less than second ... } else { // second is less than or equal to first. ... } _SubItems = value; } } private Pair _SubItems; } Given the interface constraint addition in Listing 11.22, the compilerensures that each time you use the BinaryTree class you specify a typeparameter that implements the IComparable interface. Furthermore,you no longer need to explicitly cast the variable to an IComparableinterface before calling the CompareTo() method. Casting is not evenrequired to access members that use explicit interface implementation,which in other contexts would hide the member without a cast. To resolvewhat member to call, the compiler first checks class members directly, andthen looks at the explicit interface members. If no constraint resolves theargument, only members of object are allowable. From the Library of Wow! eBook444 C hapter 11: Generics If you tried to create a BinaryTree variable using System. Text.StringBuilder as the type parameter, you would receive a compiler error because StringBuilder does not implement IComparable. The error is similar to the one shown in Output 11.3. OUTPUT 11.3: error CS0309: The type ’System.Text.StringBuilder’ must be convertible to ’System.IComparable’ in order to use it as parameter ’T’ in the generic type or method ’BinaryTree’ To specify an interface for the constraint you declare an interface con- straint. This constraint even circumvents the need to cast in order to call an explicit interface member implementation. Base Class Constraints Sometimes you might want to limit the constructed type to a particular class derivation. You do this using a base class constraint, as shown in Listing 11.23. Listing 11.23: Declaring a Base Class Constraint public class EntityDictionary : System.Collections.Generic.Dictionary where TValue : EntityBase { ... } In contrast to System.Collections.Generic.Dictionary on its own, EntityDictionary requires that all TValue types derive from the EntityBase class. By requiring the derivation, it is possible to always perform a cast operation within the generic implemen- tation, because the constraint will ensure that all type parameters derive from the base and, therefore, that all TValue type parameters used with EntityDictionary can be implicitly converted to the base. The syntax for the base class constraint is the same as that for the inter - face constraint, except that base class constraints must appear first when multiple constraints are specified. However, unlike interface constraints, From the Library of Wow! eBook 445 C onstraintsmultiple base class constraints are not allowed since it is not possible toderive from multiple classes. Similarly, base class constraints cannot bespecified for sealed classes or specific structs. For example, C# does notallow a constraint for a type parameter to be derived from string or Sys-tem.Nullable.struct/class ConstraintsAnother valuable generic constraint is the ability to restrict type parame-ters to a value type or a reference type. The compiler does not allow speci-fying System.ValueType as the base class in a constraint. Instead, C#provides special syntax that works for reference types as well. Instead ofspecifying a class from which T must derive, you simply use the keywordstruct or class, as shown in Listing 11.24.Listing 11.24: Specifying the Type Parameter As a Value Type public struct Nullable : IFormattable, IComparable, ICompa ...
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ínhTài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 332 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 323 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 307 0 0 -
70 trang 267 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 249 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 234 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 227 0 0 -
Phần III: Xử lý sự cố Màn hình xanh
3 trang 222 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 215 0 0 -
Sao lưu dữ liệu Gmail sử dụng chế độ Offline
8 trang 212 0 0