Danh mục

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    
Jamona

Hỗ trợ phí lưu trữ khi tải xuống: 35,000 VND Tải xuống file đầy đủ (98 trang) 0

Báo xấu

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

Tài liệu được xem nhiều: