Tài liệu Module 5: Common Type System docx

50 314 0
Tài liệu Module 5: Common Type System docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Contents Overview 1 An Introduction to the Common Type System 2 Elements of the Common Type System 8 Object-Oriented Characteristics 25 Lab 5: Building Simple Types 39 Review 44 Module 5: Common Type System 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. Module 5: Common Type System iii Instructor Notes After completing this module, students will be able to: ! Describe the difference between value types and reference types. ! Explain the purpose of each element in the type system, including values, objects, and interfaces. ! Explain how object-oriented programming concepts, such as abstraction, encapsulation, inheritance, and polymorphism, are implemented in the Common Type System. 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_05.ppt. Preparation Tasks To prepare for this module, you should: ! Read all of the materials for this module. ! Complete the lab. Module Strategy Use the following strategy to present this module: ! An Introduction to the Common Type System Cover the basic architecture of the Common Type System. Introduce System.Object as the root type for all types in the Microsoft .NET Framework common language runtime. Explain how all types in the Common Type System are either reference types or value types. ! Elements of the Common Type System In this section, cover the primitive types, objects, properties, and other elements of the Common Type System. The information will not be new to students with a C++ object-oriented background, so you can cover these topics quickly or omit most of the material in this section. This decision will depend on the student’s experience. ! Object-Oriented Characteristics Discuss how the object-oriented characteristics of the .NET Framework common language runtime are supported. If the class is already experienced in object-oriented techniques, you can omit this section. Presentation: 90 Minutes Lab: 45 Minutes Module 5: Common Type System 1 Overview ! An Introduction to the Common Type System ! Elements of the Common Type System ! Object-Oriented Characteristics ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** In this module, you will learn about the Common Type System. Specifically, you will learn to differentiate between value types and reference types. You will also examine how classes, interfaces, properties, methods, events, and values are represented in the Microsoft ® .NET Framework. After completing this module, you will be able to: ! Describe the difference between value types and reference types. ! Explain the purpose of each element in the type system, including values, objects, and interfaces. ! Explain how object-oriented programming concepts, such as abstraction, encapsulation, inheritance, and polymorphism, are implemented in the Common Type System. Topic Objective To provide an overview of the module topics and objectives. Lead-in In this module, you will learn about the Common Type System and object-oriented programming. 2 Module 5: Common Type System " "" " An Introduction to the Common Type System ! Common Type System Architecture ! Value Types vs. Reference Types ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** In this section, you will learn about the Common Type System architecture. Specifically, you will learn how types are specified and represented in the .NET Framework common language runtime, and you will learn the difference between value types and reference types. Topic Objective To provide an overview of the section topics. Lead-in In this section, you will learn about the architecture of the Common Type System. Module 5: Common Type System 3 Common Type System Architecture ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** A type defines characteristics for a set of values. For example, the set of whole numbers between negative 32768 and positive 32767 is called the short type in C#, or System.Int16 in the .NET Framework class library. The short type has a physical representation, which, in this case, is a 16-bit value. The short type also defines operations allowed on the type, such as addition and subtraction. System.Object System.Object is the root type for all types in the .NET Framework common language runtime. For this reason, any type you use will have System.Object as its base class. System.Object has the following methods. Methods Description Public Equals Overloaded. Determines whether two Object instances are equal. Public GetHashCode Serves as a hash function for a particular type; suitable for use in hashing algorithms and data structures, such as a hash table. Public GetType Gets the type of the object. Public ReferenceEquals Determines whether the specified Object instances are the same instance. Public ToString Returns a string that represents the current object. Protected Finalize Overridden. Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. Protected MemberwiseClone Creates a shallow copy of the current Object. For more information about the methods of System.Object, see Module 6, “Working with Types,” in Course 2349B, Programming with the Microsoft .NET Framework (Microsoft Visual C# ™ .NET). Topic Objective To describe the basic architecture of the Common Type System. Lead-in A type defines characteristics for a set of values. 4 Module 5: Common Type System Value Types Value types are any type that inherits from System.ValueType. Value types are typically simple types, such as integer values or long values. Value types are special because they are allocated on the stack. New value types are defined by using the struct or enum keywords. You cannot create value types by using the class keyword. System.ValueType inherits from System.Object, which means that all value types can behave like System.Object. The type system defines primitive types, which are Byte, Int16, Int32, Int64, Single, Double, Boolean, Char, Decimal, IntPtr, and String. There are also built-in types such as System.Array available. Reference Types Reference types are any type that inherits from System.Object and not System.ValueType. Reference types are analogous to pointers in C++, but there are subtle differences in the way reference types and pointers work. Reference types are allocated on the heap. Reference types also must be garbage collected. One example of a reference type is the System.IO file class, which represents a file in the system. Fields, Methods, and Properties The Common Type System defines fields, methods, and properties that are available to all value types and reference types. A field is a data value stored in a class. For example, Name and Age could be fields describing an employee stored in an Employee structure. A method is an operation that can be performed on a class. For example, a Tenure method could calculate how long an employee has been employed. Properties are a convenient way to expose data values to external objects and code. For example, the Name and Age fields could be exposed as properties, which would allow the Employee structure to control how the name and age are stored and retrieved. Interfaces Interfaces are the only types that do not inherit from System.Object. Interfaces have no implementation; they merely provide a description of methods, properties, and events. Any class that inherits from an interface must implement all of the methods, properties, and events in that interface. Interfaces provide a mechanism for specifying contractual relationships between classes. Interfaces also provide a means of implementing multiple inheritance. For more information about inheritance, see Module 6, “Working with Types,” in Course 2349B, Programming with the Microsoft .NET Framework (Microsoft Visual C# .NET). Module 5: Common Type System 5 Value Types vs. Reference Types ! Value Types Are Primitive or User-Defined Structures # Allocated on stack # Assigned as copies # Default behavior is pass by value ! Reference Types Are Objects That Are: # Allocated on heap using the new keyword # Assigned as references # Passed by reference ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** With the exception of interfaces, all types in the Common Type System are divided into two categories: value types and reference types. Value Types Value types represent primitive or user-defined structures. Value types are allocated on the stack and are removed from the stack when the method call in which they were declared returns. When a value type is passed as an argument to a method, it is passed by value, unless the ref keyword is used. All value types must inherit from System.ValueType. Topic Objective To explain the difference between value types and reference types. Lead-in With the exception of interfaces, all types in the Common Type System are divided into two categories: value types and reference types. 6 Module 5: Common Type System An integer is an example of a value type. The following example shows an integer value type and its effect on method calls and assignment operations. using System; public class MainClass { public static void Main() { // Create a new integer with value 5 int a = 5; // Create a new uninitialized integer int b; // Initialize b by copying the value 5 into b b = a; //Increase the value of b by 5 b += 5; Console.WriteLine("{0}, {1}", a, b); Add5(b); Console.WriteLine("{0}, {1}", a, b); } public static void Add5(int num) { num += 5; } } This code generates the following output: 5, 10 5, 10 In the preceding example, the variables a and b represent two separate instances of the integer type. The Assignment operator (=) copies the value from one variable to another. When the Add5 method is called, the = operator updates a copy of the integer, not the original integer, because a copy of the value is pushed onto the stack when passing value types. [...]... primitive types, objects, properties, and other elements of the Common Type System Module 5: Common Type System 9 Primitive Types Topic Objective To explain how primitive types are used in the common language runtime ! ! C# Support ! Primitive types in the NET Framework common language runtime are simple value types, commonly found in most programming languages Naming Differences ! Lead-in Simple Small Types... name2 and name1 are affected 7 8 Module 5: Common Type System " Elements of the Common Type System Topic Objective To provide an overview of the section topics and objectives ! Objects ! Constructors ! Properties ! Custom Types Enumerations ! In this section, you will learn about primitive types, objects, properties, and other elements of the Common Type System Primitive Types ! Lead-in ! Interfaces *****************************ILLEGAL... Unsigned 16-bit integer unsigned int32 uint System. UInt32 Unsigned 32-bit integer unsigned int64 ulong System. UInt64 Unsigned 64-bit integer 10 Module 5: Common Type System The following example shows how to create and initialize the primitive System. UInt16 type in C# to store an employee’s age: System. UInt16 age = new System. UInt16(); age = 5; Primitive types are inherently supported in C#, allowing... type to an unsigned short type: long longAge = 32; ushort ushortAge; ushortAge = (ushort) longAge; Casting in C# will automatically compile to conversion methods, such as ToUInt16 Also, you can use casting to avoid memorizing the class library names for primitive types Module 5: Common Type System 11 Objects Topic Objective To explain the role of object types in the Common Type System ! ! Fields Define... more information about encapsulation, see Encapsulation in this module 18 Module 5: Common Type System Custom Types Topic Objective To describe how to build data types that use the C# struct keyword ! ! You can build your own data type by using the C# struct keyword Are Defined with the struct Keyword in C# ! Lead-in Inherit from System. ValueType Can Have Methods, Properties, and Fields struct Employee... COuter1.CInner3.MyPublicShort = 3; //Failure because class is private, and all members are private } } 29 30 Module 5: Common Type System Inheritance Topic Objective To explain how inheritance works in the Common Type System ! Inheritance Is the Reuse of Class Members in Other Classes Lead-in ! The Common Type System Only Supports Single Inheritance for Classes ! Member Hiding Inheritance is a method of reuse.. .Module 5: Common Type System Reference Types Reference types are a reference to a value Reference types are allocated on the heap and will remain on the heap until they are garbage collected Reference types must be allocated by using the new keyword When you pass reference types as parameters, the reference is pushed onto the stack, and... Types Common in Most Languages Conversions *****************************ILLEGAL FOR NON-TRAINER USE****************************** Primitive types in the NET Framework common language runtime are simple value types, which are commonly found in most programming languages Primitive types are simple small types, such as Boolean types or character types The following table identifies all of the primitive types... share the same methods or data The common methods and data can be abstracted into a separate class that the other classes inherit from Inheritance is pervasive in the Common Type System Every class must inherit from System. Object, so all classes will have the members of System. Object Single Inheritance The Common Type System only supports single inheritance of class types Single inheritance means a class... enumeration is defined as ushort You can use any simple type for a base type, except System. Char An enumeration will be of type int, unless otherwise specified The literals are specified in a comma-delimited list Unless you specify otherwise, the first literal is assigned a value of 0, the second a value of 1, and so on 20 Module 5: Common Type System To use enumeration literals, you must fully specify . types in the Common Type System. Lead-in Everything in the Common Type System is an object and inherits from System. Object. 12 Module 5: Common Type System. primitive types, objects, properties, and other elements of the Common Type System. Module 5: Common Type System 9 Primitive Types ! Simple Small Types Common

Ngày đăng: 21/12/2013, 05:17

Tài liệu cùng người dùng

Tài liệu liên quan