- 4.1 Introduction
- 4.2 Classes, Objects, Methods, Properties and Instance Variables
- 4.3 Declaring a Class with a Method and Instantiating an Object of a Class
- 4.4 Declaring a Method with a Parameter
- 4.5 Instance Variables and Properties
- 4.6 UML Class Diagram with a Property
- 4.7 Software Engineering with Properties and set and get Accessors
- 4.8 Auto-Implemented Properties
- 4.9 Value Types vs. Reference Types
- 4.10 Initializing Objects with Constructors
- 4.11 Floating-Point Numbers and Type decimal
- 4.12 Wrap-Up
- Summary
- Terminology
- Self-Review Exercises
- Answers to Self-Review Exercises
- Exercises
- Making a Difference Exercises
4.9 Value Types vs. Reference Types
Types in C# are divided into two categories—value types and reference types. C#'s simple types (like int and double) are all value types. A variable of a value type simply contains a value of that type. For example, Fig. 4.10 shows an int variable named count that contains the value 7. Values types are implemented as structs, which are similar to classes and are discussed in more detail in Chapter 16.
Fig. 4.10 Value-type variable.
By contrast, a variable of a reference type (sometimes called a reference) contains the address of a location in memory where the data referred to by that variable is stored. Such a variable is said to refer to an object in the program. Line 11 of Fig. 4.8 creates a Grade-Book object, places it in memory and stores the object's reference in variable myGradeBook of type GradeBook as shown in Fig. 4.11. The GradeBook object is shown with its course-Name instance variable.
Fig. 4.11 Reference-type variable.
Reference-type instance variables (such as myGradeBook in Fig. 4.11) are initialized by default to the value null . string is a reference type. For this reason, string variable courseName is shown in Fig. 4.11 with an empty box representing the null-valued variable. A string variable with the value null is not an empty string, which is represented by "" or string.Empty . The value null represents a reference that does not refer to an object. The empty string is a string object with no characters in it.
A client of an object must use a variable that refers to the object to invoke (i.e., call) the object's methods and access the object's properties. In Fig. 4.8, the statements in Main use variable myGradeBook, which contains the GradeBook object's reference, to send messages to the GradeBook object. These messages are calls to methods (like DisplayMessage) or references to properties (like CourseName) that enable the program to interact with GradeBook objects. For example, the statement (in line 19 of Fig. 4.8)
|
uses the reference myGradeBook to set the course name by assigning a value to property CourseName. This sends a message to the GradeBook object to invoke the CourseName property's set accessor. The message includes as an argument the value read from the user's input (in this case, "CS101 Introduction to C# Programming") that CourseName's set accessor requires to perform its task. The set accessor uses this information to set the courseName instance variable. In Section 7.16, we discuss value types and reference types in detail.