Home > Articles > Programming > C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Reference Types

To this point, you have seen a number of different data types. C# offers two primary ways of storing information: by value (byval) and by reference (byref). The basic data types that you have learned about store information by value.

When a variable stores information by value, the variable contains the actual information. For example, when you store 123 in an integer variable called x, the value of x is 123. The variable x actually contains the value 123.

Storing information by reference is a little more complicated. If a variable stores by reference, rather than storing the information in itself, it stores the location of the information. In other words, it stores a reference to the information. For example, if x is a "by reference" variable, it contains information on where the value 123 is located. It does not store the value 123. Figure 3.1 illustrates the difference.

Figure 3.1 By reference versus by value.

The data types used by C# that store by reference are

  • Classes
  • Strings
  • Interfaces
  • Arrays
  • Delegates

Each of these data types is covered in detail throughout the rest of this book.

  • + Share This
  • 🔖 Save To Your Account