FAQ: Reference Fundamentals - Review

This community-built FAQ covers the “Review” exercise from the lesson “Reference Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

So I just finished an entire lesson explaining references, yet I still don’t understand what is the point of using them?
Lets say I have a Superclass Animal and a subclass Cat.
I create an instance of cat.
Then I create a reference to cat using the Superclass Animal.

Cat cat= new Cat();

Animal reference = cat;

So what is the point of all of this?

The second reference to the object will have only the functionalities defined for the Baseclass (a method defined for the baseclass, for en example), whilst the first reference (cat) will have access to that which was inherited from the superclass and the functionalities exclusive to the derived class.
Guess the point is: in real world scenarios, you might have references that you want only to use what is applicable to the baseclass, while maintaining some functions exclusive to the derived one.

2 Likes

I don’t understand. Why do you need the functionalities? Can you explain it better?

(@toastedpitabread), look at this:

  • Classes and interfaces are reference types . A variable of this type holds a reference to the data, not the data itself. This is different from value types like int and bool
  • The equality operator ( == ) uses a referential comparison for reference types and a value comparison for value types
    What are all the value types? What are all the reference types?

Referencing has to do with the physical address of data in memory.
One analogy uses lockers: you put items in lockers and then keep track of the data by locker number. The locker number is intrinsically independent from the item it holds.

So to check equality of reference is to ask if two locations are the same, and in contrast value comparison asks if two values (items) are the same.

Arrays for example are stored in consecutive addresses, which gives them specific types of advantages (but some disadvantages). Other data structures will work with data that is placed in separate memory locations but offer other types of efficiencies.

I have some questions:
Can variables be value types and reference types.
I know that all classes are reference types. Do value types not hold a reference but the variable related to it stores the value?
.

Yes so you can have pointer variables. They’re considered unsafe in c#.

Some reading:

1 Like

Which question does it correspond to?

or this:

.

Have you answered any of these?

Why are pointers unsafe?

In Program.cs, there are two lines that are commented out:

f.Define();
bdiss3.Define();

Before you move on, make sure you can explain why each of them cause an error.

Yeah I don’t actually know. This module has been a little confusing. I’m not sure if it’s me, but I would have found being able to reference the IFlippable and Book code throughout a help but they disappeared after the first lesson. I guess the answer is something like “you’re trying to do something that doesn’t exist in Book or IFlippable” but I have no idea.