Thứ Bảy, 8 tháng 2, 2014

Tài liệu Module 8: Using Reference-Type Variables ppt

Module 8: Using Reference-Type Variables v


Module Strategy
Use the following strategy to present this module:
 Using Reference-Type Variables
Some of this material will be familiar to students. For example, they learned
about the new keyword when discussing class instantiation. It is important
that you explain the basic memory management of reference types in this
section.
 Using Common Reference Types
You need to point out that reference types available in C# are part of the
.NET Framework class library.
This section explains that exceptions and strings are reference types, and
provides some basic information about common methods of these types.
Most of the material in this section relates to the string type and covers
those methods that are likely to be of most use to students when they do the
exercises in the labs.
 The Object Hierarchy
This section explains that every reference type inherits from object. You do
not need to thoroughly discuss inheritance in this module because it is
covered in detail in other modules, but you might need to explain the
concept briefly if some of the students have not encountered it before.
The Reflection topic might be difficult for less experienced students, as its
value might not be immediately obvious. It is sufficient for you to explain
that the mechanism exists and that a little more information is provided in
Module 14, “Attributes,” in Course 2124C, Programming with C#.
 Namespaces in the .NET Framework
This is not intended to be a complete listing of everything in the .NET
Framework. You need to stress that there are many useful features provided
by .NET and that in this course you can only introduce students to some of
the more interesting and valuable ones. When delivering the material, it is
important that you demonstrate how to find information in the Microsoft
Visual Studio
® .NET Help documents.
 Data Conversions
To understand the rules for converting reference types, students will need to
have a basic knowledge of inheritance.


Module 8: Using Reference-Type Variables 1


Overview
 Using Reference-Type Variables
 Using Common Reference Types
 The Object Hierarchy
 Namespaces in the .NET Framework
 Data Conversions

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
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 System.Object class 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:
 Describe the important differences between reference types and value types.
 Use common reference types, such as string.
 Explain how the object type works and become familiar with the methods it
supplies.
 Describe common namespaces in the Microsoft® .NET Framework.
 Determine whether different types and objects are compatible.
 Explicitly and implicitly convert data types between reference types.
 Perform boxing and unboxing conversions between reference and value
data.

Topic Objective
To provide an overview of
the module topics and
objectives.
Lead-in
In this module, you will learn
how to use reference types
in C# and you will learn
about the C# object
hierarchy.
2 Module 8: Using Reference-Type Variables




 Using Reference-Type Variables
 Comparing Value Types to Reference Types
 Declaring and Releasing Reference Variables
 Invalid References
 Comparing Values and Comparing References
 Multiple References to the Same Object
 Using References as Method Parameters

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
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.
After completing this lesson, you will be able to:
 Describe the important differences between reference types and value types.
 Use and discard reference variables.
 Pass reference types as method parameters.

Topic Objective
To provide an overview of
the topics covered in this
section.
Lead-in
In this lesson, you will learn
how to use reference types
in C#.
Delivery Tip
Although there is some
explanation of how memory
is released from reference
variables, this module does
not contain a full discussion
of garbage collection.
Module 8: Using Reference-Type Variables 3


Comparing Value Types to Reference Types
 Value types
 The variable
contains the
value directly
 Examples:
char, int
42
42
int mol;
mol = 42;
int mol;
mol = 42;


string mol;
mol = "Hello";
string mol;
mol = "Hello";
Hello
Hello
 Reference types
 The variable contains a
reference to the data
 Data is stored in a
separate memory area

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
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.
Topic Objective
To compare value types to
reference types.
Lead-in
Most values and variables
that you have encountered
so far in this course are
value types. Here is a
comparison between value
types and reference types.
Delivery Tip
You might want to explain
that reference types are not
the same as reference
parameters, despite the
similarity in the names.

It is probably also worth
discussing why C# has
value types at all: they are
efficient and they are
allocated on the stack. As a
consequence of this, value
types do not require
garbage collection.
Delivery Tip
C and C++ developers
might compare reference
variables to pointers to
objects. This is a helpful
comparison, but point out
that C# reference variables
are completely type-safe
and cannot point to invalid
objects. Also, C and C++ do
not have automatic garbage
collection.
4 Module 8: Using Reference-Type Variables


Declaring and Releasing Reference Variables
 Declaring reference variables
coordinate c1;
c1 = new coordinate();
c1.x = 6.12;
c1.y = 4.2;
coordinate c1;
c1 = new coordinate();
c1.x = 6.12;
c1.y = 4.2;


6.12
6.12
4.2
4.2
c1 = null;
c1 = null;


6.12
6.12
4.2
4.2
 Releasing reference variables

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
To use reference-type variables, you need to know how to declare and initialize
them and how to release them.
Declaring Reference Variables
You declare reference-type variables by using the same syntax that you use
when declaring value-type variables:
coordinate c1;

The preceding example declares a variable c1 that can hold a reference to an
object of type coordinate. However, this variable is not initialized to reference
any coordinate objects.
To initialize a coordinate object, use the new operator. This creates a new
object and returns an object reference that can be stored in the reference
variable.
coordinate c1;
c1 = new coordinate( );

If you prefer, you can combine the new operator with the variable declaration
so that the variable is declared and initialized in one statement, as follows:
coordinate c1 = new coordinate( );

After you have created an object in memory to which c1 refers, you can then
reference member variables of that object by using the dot operator as shown in
the following example:
c1.x = 6.12;
c1.y = 4.2;

Topic Objective
To show how reference
variables are declared and
initialized.
Lead-in
To use reference-type
variables, you need to know
how to declare and initialize
them and how to release
them.
For Your Information
In C and C++, you need to
use a special ->operator to
remove a reference to a
pointer and access a
member. There is no
analogous requirement in
C#.
Module 8: Using Reference-Type Variables 5


Example of Declaring Reference Variables
Classes are reference types. The following example shows how to declare a
user-defined class called coordinate. For simplicity, this class has only two
public member variables: x and y.
class coordinate
{
public double x = 0.0;
public double y = 0.0;
}

This simple class will be used in later examples to demonstrate how reference
variables can be created, used, and destroyed.
Releasing Reference Variables
After you assign a reference to a new object, the reference variable will
continue to reference the object until it is assigned to refer to a different object.
C# defines a special value called null. A reference variable contains null when
it does not refer to any valid object. To release a reference, you can explicitly
assign the value null to a reference variable (or simply allow the reference to go
out of scope).
Delivery Tip
This class has public data
items that are not well
encapsulated, but this
makes the example simple.
Delivery Tip
Garbage collection is not
covered at this point in the
course, so you do not need
to go into details about the
reclaiming of memory. You
might want to mention it
briefly.
For Your Information
In C and C++, the nearest
equivalent to the C# null is
the NULL macro. In
Microsoft Visual Basic®, the
nearest equivalent is
Nothing. (Null in Visual
Basic is used to represent
null fields in databases and
has nothing to do with object
variables.)
6 Module 8: Using Reference-Type Variables


Invalid References
 If you have invalid references
 You cannot access members or variables
 Invalid references at compile time
 Compiler detects use of uninitialized references
 Invalid references at run time
 System will generate an exception error

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
You can only access the members of an object through a reference variable if
the reference variable has been initialized to point to a valid reference. If a
reference is not valid, you cannot access member variables or methods.
The compiler can detect this problem in some cases. In other cases, the problem
must be detected and handled at run time.
Invalid References at Compile Time
The compiler is able to detect situations in which a reference variable is not
initialized prior to use.
For example, if a coordinate variable is declared but not assigned, you will
receive an error message similar to the following: “Use of unassigned local
variable c1.” The following code provides an example:
coordinate c1;
c1.x = 6.12; // Will fail: variable not assigned

Topic Objective
To explain how the compiler
and run-time system handle
invalid references.
Lead-in
You cannot access member
variables or methods if the
reference is not valid.
Module 8: Using Reference-Type Variables 7


Invalid References at Run Time
In general, it is not possible to determine at compile time when a variable
reference is not valid. Therefore, C# will check the value of a reference variable
before it is used, to ensure that it is not null.
If you try to use a reference variable that has the value null, the run-time system
will throw a NullReferenceException exception. If you want, you can check
for this condition by using try and catch. The following is an example:
try {
c1.x = 45;
}
catch (NullReferenceException) {
Console.WriteLine("c1 has a null value");
}

Alternatively, you can check for null explicitly, thereby avoiding exceptions.
The following example shows how to check that a reference variable contains a
non-null reference before trying to access its members:
if (c1 != null)
c1.x = 45;
else
Console.WriteLine("c1 has a null value");

8 Module 8: Using Reference-Type Variables


Comparing Values and Comparing References
 Comparing value types
 == and != compare values
 Comparing reference types
 == and != compare the references, not the values


1.0
1.0
2.0
2.0


1.0
1.0
2.0
2.0
Different

*****************************
ILLEGAL FOR NON-TRAINER USE******************************
The equality (==) and inequality (!=) operators might not work in the way you
expect for reference variables.
Comparing Value Types
For value types, you can use the == and != operators to compare values.
Comparing Reference Types
For reference types other than string, the == and != operators determine
whether the two reference variables are referring to the same object. You are
not comparing the contents of the objects to which the variables refer. For string
type, == compares the value of the strings.
Topic Objective
To show the default
implementation of the ==
and != operators for
reference variables.
Lead-in
The == and != operators
might not work in the way
you expect for reference
variables.

Không có nhận xét nào:

Đăng nhận xét