Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Encapsulation and Arrays Encapsulation

Brooks G
Brooks G
5,284 Points

What's the difference between changing the field and changing items in the array (in reference to _path)?

The video mentions that we use "readonly" because "it only prohibits overwriting the field with different value" (very straightforward, as is using "private" to deny direct access to others who might create a Path object), but that "it doesn't prohibit anyone from changing the individual items in the array."

What is the difference between changing the field and changing individual items in the array's index in this context?

For example, if "readonly int x = 4," a user can't change the value to "6." For an array of integers (say, int[] x = {1, 2, 3};), I'm drawing a blank as to how we could alter individual integers in the array and not be "overwriting the field," despite having it set to readonly. Which part of the field is readonly denying access to if I can change every element in the array?

Is he referring to constructor initialization when he says that the array's index can be changed -- and they can't be changed once the object is constructed?

Thank you!!

Edit: See comment below.

Brooks G
Brooks G
5,284 Points

So, in toying around, I made a couple of fields and methods within one of the classes we already constructed:

I set an array of integers, "private readonly int[] x = {1, 2, 3};"

I made a couple of methods within the class: One of them extracts the value and assigns it to a new variable in Main. The other takes the data from a variable declared in main and assigns it.

It would seem that I can't create an array of integers in Main, "y," and assign the entire array to "x" with my assignation method (compiler error), but I'm able to take single elements of "y" and assign them to "x" without an issue (specifying that I'm only changing x[0] to y[0], for example, rather than x = y).

I'm not totally sure how that relates to the Path class & our array of MapLocations given creating a method to alter the array could potentially destroy the path (probably just mentioning for the sake of knowing), but I'm 99.9999% sure that's what he was getting at.

Can anyone expand on why that's the case? I'm assuming it has to do with memory allocation (i.e. memory is allocated to create the object, so we can alter individual values given it doesn't require any additional memory, but we can't change the entire array because that might entail changing the size of the array, and thus tapping into memory that wasn't assigned to this particular object).

1 Answer

Steven Parker
Steven Parker
231,275 Points

The variable doesn't hold the array itself, but a reference to the array. So being "readonly" prevents the variable from being assigned another reference (or null), but it has no effect on access to the array elements.

If you need an array that cannot be changed at all, you could keep it in a private member and only allow read access with special methods. And if you're working with .NET core, it provides a special ImmutableArray class.

Brooks G
Brooks G
5,284 Points

Thanks for clarifying!

Are all basic data types (e.g. int, char, double -- not objects, strings and the like) stored in variables as pointers/references in C#, or is that unique to arrays/lists/etc.?

To my understanding, those would be considered "value type" and are stored on the stack (not as a pointer), whereas arrays, objects/structs, and strings are "reference" type, and stored on the heap.

Hopefully I'm not too far off base.

Steven Parker
Steven Parker
231,275 Points

You're quite right, the primitive types are stored as values. So when those are readonly, the value itself cannot be changed.

Brooks G
Brooks G
5,284 Points

Awesome, all seems to make sense! Thank you for the help!