Understanding Reference Types

Reference types are represented by classes. Classes are probably the most important items in modern programming languages and are the basis of object-oriented programming. Reference types have one big difference from value types. Variables that declare a reference type do not store the data of the type itself; they just store an address to the data. In other words, they are just pointers to the data. To better explain (and understand) this fundamental concept, let’s look at an example. Consider the following class, Person, which exposes two simple properties:

Class Person
    Property FirstName As String
    Property LastName As String
End Class

You need to create an instance of such a class so that you can store data (in this case, setting properties) and then manipulate the same data. You can do this by using the following lines of code:

Dim onePerson As New Person
onePerson.FirstName = "Alessandro"
onePerson.LastName = "Del Sole"


Strongly Typed Objects

In .NET development, you often encounter the term strongly typed. To understand this term, let’s look at an example. The onePerson object in the preceding code is strongly typed because it is of a certain type, Person. This means that onePerson can accept an assignment only from compliant objects, such as other Person objects. Such restriction is important because it prevents errors and problems. Moreover, the compiler knows how to treat such a specialized object. A variable of type Object is not strongly typed because it is just of the root type and is not specialized. Object can accept anything, but without restrictions, the use of non-strongly typed objects could lead to significant problems. Chapter 14 discusses generics, and at that point, you’ll get a more thorough understanding of strongly typed objects.


Now you have an instance of the Person class, named onePerson. Consider the following line of code:

Dim secondPerson As Person = onePerson

A new object of type Person (secondPerson) is declared and is assigned with the onePerson object. Because of the equality operator, you would probably expect secondPerson to be an exact copy of onePerson. You could consider at this point some edits to the secondPerson object; for example, we could modify the first name:

secondPerson.FirstName = "Alex"

You can try to check the result of the previous operations by simply writing the output to the Console window. Try writing the result of secondPerson, like so:

Console.WriteLine(secondPerson.FirstName)
Console.WriteLine(secondPerson.LastName)
Console.ReadLine()

As you might correctly expect, the preceding code produces the following result:

Alex
Del Sole

Now you can simply write the result for onePerson to the Console window:

Console.WriteLine(onePerson.FirstName)
Console.WriteLine(onePerson.LastName)
Console.ReadLine()

This code produces the following result:

Alex
Del Sole

As you can see, editing the first name in secondPerson also affected onePerson. This means that secondPerson is not a copy of onePerson. It is instead a copy of the reference to the actual data. Now you should have a better understanding of reference types.

As their name implies, reference types have an address in memory where data is stored, and variables declaring and instantiating reference types just hold a reference to that data. To get a real copy of data, you can write something like this:

Dim secondPerson As New Person
secondPerson.FirstName = onePerson.FirstName
secondPerson.LastName = onePerson.LastName

Then you can edit secondPerson’s properties, ensuring that this will not affect the onePerson object. This kind of technique for creating a clone for a reference type is good with objects exposing only a few properties, but fortunately there are more interesting techniques for cloning more complex objects. These are discussed in the “Deep Copy and Shallow Copy” section, later in this chapter.


String Type in the .Net Framework

In the .NET Framework, String is a reference type, but it’s actually treated as a value type, as explained shortly.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.103.183