FAQ: Reference Fundamentals - Arrays of References

This community-built FAQ covers the “Arrays of References” 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 Arrays of References

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!

The exercise doesn’t follow what the explanation says, while completely defeating the purpose of what has been explained in this lesson.
The starting code is this:

static void Main(string[] args)
{
Dissertation diss1 = new Dissertation(32, "Anna Knowles-Smith", "Refugees and Theatre");
Dissertation diss2 = new Dissertation(19, "Lajos Kossuth", "Shiny Happy People");
Diary dy1 = new Diary(48, "Anne Frank", "The Diary of a Young Girl");
Diary dy2 = new Diary(23, "Lili Elbe", "Man into Woman");
}

and the instructions tell me to create an array that contains diss1, diss2, dy1 and dy2.
This is what it accepts:
Book[] books = new Book[] {diss1, diss2, dy1, dy2}

Wouldn’t it be faster and better to just do this?

Book[] books = new Book[] 
{
new Dissertation(32, "Anna Knowles-Smith", "Refugees and Theatre"),
new Dissertation(19, "Lajos Kossuth", "Shiny Happy People"),
new Diary(48, "Anne Frank", "The Diary of a Young Girl"),
new Diary(23, "Lili Elbe", "Man into Woman")
};

and then the foreach loop still returns the same result.

1 Like

Instead of using an interface as suggested in the explanation to create an array, could an abstract class be used?

1 Like

i too think the same …

1 Like

What does your foreach loop look like?

I can’t get mine to print to the console.
This is mine:

foreach ( Book Title in books)
{
Console.WriteLine(Title);
}

I figured it out. lol

image

I did everything correctly but the console throws error CS1922, maybe because there is only program.cs available to us and other class cs files are hidden, this is dogshit really. Whole time it’s bugging me. Give me everything there is smh.

So not only do you INCORRECTLY create an array in the article but you also make me do the error and tell me it was correct even if it isn’t! Ridiculous, this course has been done by dogs!
IFlippable[ ] classroom = new IFlippable{};
WHERE ARE YOUR BOX BRACKETS IN FRONT OF CURLY BRACKETS?
I’ve been stuck for an hour or two wondering what the ■■■■ is wrong, I am sick of this.

It’d be better to change the variable name from Title to book.
Because we’re looping through classes, Title works but it might be confusing to others when they read your codes.
Capture

Or another example

int[] numbers = new int[] { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
    Console.WriteLine(number);
    // 1, 2, 3, 4, 5
}
1 Like

I agree @kekpop, I was like “Whaaaaaaat???!!!” When I saw this, how could the devs forget the square brackets before the curly braces???

1 Like

@8-bit-gaming, feel like writing this one up? The instructions show as an example:

image

Needs to be either of the following:
image
or
image

Probably the first would be more consistent with the exercise, but the way it’s currently written throws the following error when learners try to follow the example code pattern.
image

1 Like

Sure thing!!
I just wrote out a report!

Thanks for posting this @kekpop.
It was most likely just a typo, and will soon be fixed.

@kekpop and @hypercoder457, hopefully you will be happy to know that this has been fixed!

Thanks again for the feedback. :slightly_smiling_face:

Thanks @8-bit-gaming

1 Like

If you want to access the other files to check the code there, you can always open the folder on top left corner where you enter your code and click on the file you want, it will open a new tab with the code inside. All codes are accessibles that way in every lesson.

It wouldn’t, on the contrary. What you’ve done is added 4 unnamed references to an array. References should be stored in a variable whenever possible since you are probably gonna use them more than once and do with them more than just write something out in terminal.
In Codecademy example you can access info under dy1 and diss1 knowing what you are accessing and it will make your life a lot easier when debugging, which you will be doing most of the time in app building :sweat_smile:

Any reason that I’m missing as to why the first book title prints twice and the second one does not print at all when running the code:

Book books = new Book { diss1, diss1, dy1, dy2 };
foreach(Book book in books)
{
Console.WriteLine(book.Title);
}

But all steps show as completed correctly.

because you wrote twice diss1 instead of diss1, diss2

No, I don’t think so, abstract classes can’t be instantiated. If you try to use an abstract class then you can’t make it as the array element type since abstract classes can’t be instantiated.
Is that right?

I feel like the intro to this is in conflict with what it wants you to do in the example.

Instead of dealing with individual variables, we can use an array of IFlippable references

I thought that we would be putting the new Dissertation and new Book and so on in an array of Book references. But instead we literally need to just reference the variables in an array of Book references. It seems to be contrary to what the introduction is telling us is more efficient.