Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 48 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
48
Dung lượng
828,41 KB
Nội dung
Contents Overview 1 System.Object Class Functionality 2 Specialized Constructors 12 Type Operations 18 Interfaces 28 Managing External Types 34 Lab 6:WorkingwithTypes 38 Review 43 Module6:WorkingwithTypes Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. 2001-2002 Microsoft Corporation. All rights reserved. Microsoft, ActiveX, BizTalk, IntelliMirror, Jscript, MSDN, MS-DOS, MSN, PowerPoint, Visual Basic, Visual C++, Visual C#, Visual Studio, Win32, Windows, Windows Media, and Window NT are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Module6:WorkingwithTypes iii Instructor Notes After completing this module, students will be able to: ! Apply attributes to control visibility and inheritance in classes and interfaces. ! Create and use interfaces that define methods and properties. ! Explain how boxing and unboxing works and when it occurs. ! Use operators to determine types at run time and cast values to different types. ! Explain what features are available to work with unmanaged types, such as COM types. Materials and Preparation This section provides the materials and preparation tasks that you need to teach this module. Required Materials To teach this module, you need the Microsoft ® PowerPoint ® file 2349B_06.ppt. Preparation Tasks To prepare for this module, you should: ! Read all of the materials for this module. ! Complete the lab. Presentation: 75 Minutes Lab: 45 Minutes iv Module6:WorkingwithTypesModule Strategy Use the following strategy to present this module: ! System.Object Class Functionality In this section, discuss how System.Object provides classes to generate hash functions, represent strings, and compare objects for identity and equality. Explain that this section does not cover all of the classes in System.Object, and that other classes are covered elsewhere in the course. For example, finalization and the Finalize method are covered in detail in Module 9, “Memory and Resource Management,” in Course 2349B, Programming with the Microsoft .NET Framework (Microsoft Visual C# ™ .NET). ! Specialized Constructors This section covers more advanced types of constructors. Explain how static constructors work, when to use them, and when to use private constructors. If the students in your class already have experience in C++, you may not need to spend much time on these topics. ! Type Operations The Microsoft .NET Framework common language runtime supports a variety of type operations for workingwith types. Discuss conversions and conversion operators for determining and converting the type of an object. Also cover how to cast types for conversion and for treating a type as a different type. For experienced C++ programmers, you may be able to cover type conversion and casting quickly. C++ programmers may find it useful that the as operator in C# is similar to dynamic_cast in C++. Spend most of this section discussing boxing and unboxing. Students will need to be aware of the performance consequences of boxing. Explain how you can avoid or minimize these consequences if you must use boxing. ! Interfaces Discuss how multiple inheritance works through interfaces and explain how to explicitly implement interfaces. As with the other topics in this module, you may be able to cover this section quickly if your audience is already familiar with object-oriented programming techniques. For experienced C++ programmers, consider mentioning that explicit interface implementation was not possible in C++. ! Managing External Types Briefly introduce Platform Invocation Services and COM interoperability. Be aware that more information about these topics is available in Module 15, “Interoperating Between Managed and Unmanaged Code,” in Course 2349B, Programming with the Microsoft .NET Framework (Microsoft Visual C# .NET) and “Interoperating with Unmanaged Code” in the .NET Framework SDK documentation. Module6:WorkingwithTypes 1 Overview ! System.Object Class Functionality ! Specialized Constructors ! Type Operations ! Interfaces ! Managing External Types ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** In this module, you will learn how to apply your knowledge of the Common Type System to various programming scenarios. This module will help you understand how to use types efficiently when developing Microsoft ® .NET Framework applications. You should understand that many nuances in the type system can affect program clarity and performance if ignored. This module covers the use of attributes to control visibility and inheritance on types and explains how to work with various type operations, such as boxing and unboxing, and type operators. The module then explores how to work withtypes programmatically by using operators to coerce, cast, or discover types at run time. In addition, this module discusses how to build an interface that supports methods and properties and how to make interface designs more efficient. The module also highlights features that are designed to help you work with unmanaged types, such as COM types. After completing this module, you will be able to: ! Apply attributes to control visibility and inheritance in classes and interfaces. ! Create and use interfaces that define methods and properties. ! Explain how boxing and unboxing works and when it occurs. ! Use operators to determine types at run time and cast values to different types. ! Explain what features are available to work with unmanaged types, such as COM types. Topic Objective To provide an overview of the module topics and objectives. Lead-in In this module, you will learn how to apply your knowledge of the Common Type System to various programming scenarios. 2 Module6:WorkingwithTypes " "" " System.Object Class Functionality ! Hash Codes ! Identity ! Equality ! String Representation ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** In this section, you will learn about the common methods that you need to override on the System.Object class. This section does not cover finalization and the Finalize method, which are covered in detail in Module 9, “Memory and Resource Management,” in Course 2349B, Programming with the Microsoft .NET Framework (Microsoft Visual C# ™ .NET). Also, this section does not cover the MemberwiseClone method. Topic Objective To provide an overview of the topics covered in this section. Lead-in In this section, you will learn about the common methods that you need to override on the System.Object class. Module6:WorkingwithTypes 3 Hash Codes ! Hash Code Used to Perform Quick Lookups ! Override GetHashCode Method on System.Object ! Should Return Same Hash Code for Objects of Same Value ! Should Implement Efficient Algorithm struct Student { string name; int ID; //Unique for each instance public override int GetHashCode() { return ID; } } struct Student { string name; int ID; //Unique for each instance public override int GetHashCode() { return ID; } } ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** A hash function is used to quickly generate a number, or hash code, that corresponds to the value of an object. Hash codes are useful for performing quick lookups in tables, such as the HashTable class, and other kinds of collections. A hash table uses the hash code to drastically limit the number of objects that must be searched to find a specific object in a collection of objects. The hash table does this by getting the hash value of the object and eliminating all objects with a different hash code. This preliminary search leaves only those objects with the same hash code to be searched. Because there are few instances with that hash code, searches are much quicker. System.Object provides a GetHashCode method, which returns an int type. You should override this method to return a hash code on any custom classes or structures that you create. One reason for overriding this method is that when two objects are equal in value, you should get the same hash code for each object if you call GetHashCode. In the case of custom objects, the default implementation of GetHashCode does not give you the same hash code for two objects that are equal in value. Topic Objective To explain how to use hash codes to perform quick lookups in tables and other types of collections. Lead-in A hash function is used to quickly generate a number, or hash code, that corresponds to the value of an object. 4 Module6:WorkingwithTypes A good hash code algorithm will support the best performance by generating a random distribution for all input. You should base your hash code algorithm on one of the unique fields in the class. Also, you should never throw an exception from the GetHashCode method because GetHashCode can be called frequently and should always work reliably. The following example shows how to implement GetHashCode for a Student structure that stores a student’s name and ID. struct Student { string name; int ID; //Unique for each instance public override int GetHashCode() { return ID; } } Module6:WorkingwithTypes 5 Identity ! Compare to Determine If Two References Are Actually the Same Object ! Use the Object.ReferenceEquals Method to Test Identity ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** There are two kinds of comparison for objects: identity and equality. This topic covers object identity. Determining Identity Two objects are identical if they are, in fact, the same object. Every object in the .NET Framework common language runtime has an identity that makes it unique in the system. In C++, an object’s identity is determined by its address. Thus, if two pointers are compared and contain the same address, they point to the same object. In COM, an object’s identity is determined by the IUnknown interface. Thus, if two IUnknown interface pointers are compared and contain the same address, they are the same COM object. In the .NET Framework common language runtime, you can use the Object.ReferenceEquals method to compare for identity. Internally, ReferenceEquals compares the addresses of the objects in memory to determine if they are the same object. If they are the same object, ReferenceEquals returns true. Topic Objective To explain how identity is determined in the .NET Framework common language runtime. Lead-in There are two kinds of comparison for objects: identity and equality. This topic covers object identity. 6 Module6:WorkingwithTypes Using the Object.ReferenceEquals Method In the following example, a value type variable called x is created and passed in two parameters to the Test method. The Test method compares the two parameters to determine if they are identical. class MyObject { public int X; } class MainClass { public static void Main() { MyObject obj1 = new MyObject(); obj1.X = 5; Test(obj1, obj1); MyObject obj2 = new MyObject(); obj2.X = 5; Test(obj1, obj2); } public static void Test(MyObject a, MyObject b) { if (Object.ReferenceEquals(a,b)) Console.WriteLine("Identical"); else Console.WriteLine("Not Identical"); } } This code generates the following output: Identical Not Identical [...]... variety of type operations for workingwithtypes This section discusses conversions and conversion operators for determining and converting the type of an object This section also discusses how to cast types for conversion and for treating a type as a different type It also describes boxing and unboxing value types to treat them as reference types Module 6:WorkingwithTypes 19 Conversions Topic Objective... class with static members is different than an interface The static members can contain implementations, a class can have static fields, but an interface cannot 18 Module6:WorkingwithTypes " Type Operations Topic Objective To provide an overview of the topics covered in this section Lead-in The NET Framework common language runtime supports a variety of type operations for workingwithtypes !... //Calculate and return tan(x) } private Trig(){} } Module6:WorkingwithTypes 17 Classes with all static members can be used to maintain global algorithms, as shown in the preceding example They can also be used as a singleton class, which has only one set of values and methods that is available while a program is running To derive from a class with static members, you should mark the constructor... disassembly, it has a different name, cctor, which stands for class constructor In disassembly, instance constructors are called ctor You can have both types of constructors in the same class Module 6:WorkingwithTypes 15 If you initialize a static field inline with a value, a static constructor is created automatically The following example shows how the DeviceConnection class can be rewritten to use an... wrote in the static constructor runs 16 Module6:WorkingwithTypes Private Constructors Topic Objective To explain when to use private constructors ! Prevent a Class from Being Instantiated Lead-in ! Use Them on Classes with All Static Members ! Use a Protected Constructor to Inherit from the Class A private constructor can never be called Therefore any class with a private constructor cannot be instantiated... constructors *****************************ILLEGAL FOR NON-TRAINER USE****************************** This section covers more advanced types of constructors It explains how static constructors work, when to use them, and when to use private constructors Module 6:WorkingwithTypes 13 Static Constructors Topic Objective To explain how static constructors work Lead-in Static constructors are used to initialize... program begins running but before the first instance of the class is created 14 Module6:WorkingwithTypes The following example shows a DeviceConnection class that represents a generic connection to a device The class maintains a static field that holds the current connection count The static constructor is created with the same name as the class but has the static attribute, rather than the public.. .Module 6:WorkingwithTypes 7 Equality Topic Objective To explain how to override the Equals method and to introduce guidelines for implementing code to provide equality comparison for types Lead-in ! Comparing Two Objects to Determine If They Are Equal ! Override the Equals Method ! Supply ==... to perform appropriate behavior when comparing objects of certain types The default Object.Equals method calls Object.ReferenceEquals, which results in an identity comparison instead of a value comparison In general, you should also override the == and != operators to allow easier syntax to compare objects 8 Module6:WorkingwithTypes The following example shows how to override the Equals method... Console.WriteLine(r1.Equals(r2)); Console.WriteLine(r1 == r2); Console.WriteLine(null == r1); } } Module6:WorkingwithTypes 9 This code generates the following output: True True False Guidelines for Equality Comparison Use the following guidelines when implementing code to provide equality comparison for types ! Anytime you override the Equals method, also override the GetHashCode method If two objects . Operations 18 Interfaces 28 Managing External Types 34 Lab 6: Working with Types 38 Review 43 Module 6: Working with Types Information in this document, including. of the materials for this module. ! Complete the lab. Presentation: 75 Minutes Lab: 45 Minutes iv Module 6: Working with Types Module Strategy Use the following