Learn
What do you think will be printed by this code?
Dissertation diss1 = new Dissertation(); Dissertation diss2 = diss1; diss1.CurrentPage = 0; diss2.CurrentPage = 16; Console.WriteLine(diss1.CurrentPage); Console.WriteLine(diss2.CurrentPage);
Did you guess 0
and 16
? The answer is 16
and 16
!
To understand what’s happening here, we need to understand references. They might not be obvious in this code, but learning how to use them unlocks a whole new set of superpowers in C#. This lesson explains:
- How references work in C#
- Why references are useful
- How to upcast a reference
- How to create a null reference
Instructions
This lesson uses the Book
, Dissertation
, and Diary
classes and the IFlippable
interface. Look through each file to get acquainted.
IFlippable
is the interfaceBook
is the base classDissertation
andDiary
are classes that extend the base class and implement the interface
If you need to refresh yourself with interfaces and inheritance, take the C# Interfaces and Inheritance lessons.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.