|
|
Contents
Overview 1
Using Reference -Type Variables 2
Using Common Reference Types 15
The Object Hierarchy 23
Namespaces in the .NET Framework 29
Lab 8.1: Defining And Using Reference -
Variables 35
Data Conversions 43
Multimedia: Type-Safe Casting 56
Lab 8.2 Converting Data 57
Review 63
Module 8: Using
Reference-Type Variables
This course is based on the prerelease Beta 1 version of Microsoft? Visual Studio .NET.
Content in the final release of the course may be different from the content included in this
prerelease version. All labs in the course are to be completed with the Beta 1 version of
Visual Studio .NET.
Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
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 Microsoft Corporation. All rights reserved.
Microsoft, ActiveX, BizTalk, IntelliSense, JScript, Microsoft Press, MSDN, PowerPoint, Visual
Basic, Visual C++, Visual C#, Visual Studio, Windows, and Windows Media are either registered
trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
Other product and company names mentioned herein may be the trademarks of their respective
owners.
Module 8: Using Reference-Type Variables 1
Overview
n Using Reference-Type Variables
n Using Common Reference Types
n The Object Hierarchy
n Namespaces in the .NET Framework
n Data Conversions
In this module, you will learn how to use reference types in C#. You will learn
about a number of reference types, such as string, that are built into the C#
language and run-time environment. These are discussed as examples of
reference types.
You will also learn about the C# object hierarchy and the object type in
particular, so you can understand how the various reference types are related to
each other and to the value types. You will learn how to convert data between
reference types by using explicit and implicit conversions. You will also learn
how boxing and unboxing conversions convert data between reference types
and value types.
After completing this module, you will be able to:
n Describe the important differences between reference types and value types.
n Use common reference types, such as string.
n Explain how the object type works and become familiar with the methods it
supplies.
n Describe common namespaces in the Microsoft? .NET Framework.
n Determine whether different types and objects are compatible.
n Explicitly and implicitly convert data types between reference types.
n Perform boxing and unboxing conversions between reference and value data.
2 Module 8: Using Reference-Type Variables
u Using Reference-Type Variables
n Comparing Value Types to Reference Types
n Declaring and Releasing Reference Variables
n Invalid References
n Comparing Values and Comparing References
n Multiple References to the Same Object
n Using References as Method Parameters
Reference types are important features of the C# language. They enable you to
write complex and powerful applications and effectively use the run-time
framework.
In this section, you will learn about reference-type variables and about how
they are different from value-type variables. You will learn how to use and
discard reference variables. You will also learn how to pass reference types as
method parameters.
Module 8: Using Reference-Type Variables 3
Comparing Value Types to Reference Types
n Value Types
l The variable
contains the
value directly
l Examples:
char, int
4422
int mol;
mol = 42;
int mol;
mol = 42; ??
string mol;
mol = "Hello";
string mol;
mol = "Hello";
HHeelllloo
n Reference Types
l The variable contains a
reference to the data
l Data is stored in a
separate memory area
C# supports basic data types such as int, long and bool. These types are also
referred to as value types. C# also supports more complex and powerful data
types known as reference types.
Value Types
Value-type variables are the basic built- in data types such as char and int. Value
types are the simplest types in C#. Variables of value type directly contain their
data in the variable.
Reference Types
Reference-type variables contain a reference to the data, not the data itself. The
data itself is stored in a separate memory area.
You have already used several reference types in this course so far, perhaps
without realizing it. Arrays, strings, and exceptions are all reference types that
are built into the C# compiler and the .NET Framework. Classes, both built-in
and user-defined, are also a kind of reference type. |
|