DEV Community

Discussion on: C# For Beginners - Lesson 9: Arrays

Collapse
 
coding_mama profile image
Kristina (Coding Mama) • Edited

Hi, thanks for reading!

I'm using Dotnet Interactive Notebooks extension in VSCode. It's linked in this article: dev.to/coding_mama/c-for-beginners...

Regarding question 2, it's entirely possible to make that assumption! However, if it's an object array technically the elements are not int or string but would be object type. So the statement would still be false.

Thanks again for your comment! 😊

Collapse
 
davidkroell profile image
David Kröll

Thanks for your reply!

Hmm, I can't fully agree with your above argument, since the underlying (concrete) type does not change when you store something inside a object array.

See the following example:

var arr = new object[] {"some string", 42};

Console.WriteLine($"Type: {arr[0].GetType()}, Value: {arr[0]}");
Console.WriteLine($"Type: {arr[1].GetType()}, Value: {arr[1]}");

// Output: 
// Type: System.String, Value: some string
// Type: System.Int32, Value: 42
Enter fullscreen mode Exit fullscreen mode

Other types for the array would also work, they just have to satisfy both int and string, for example IComparable.

Thread Thread
 
coding_mama profile image
Kristina (Coding Mama)

Ah, in that case I stand corrected 😊 though I haven't really introduced the object type in this course, so the expected answer was false. I should probably reword that question to make it exact!

Thanks for the clarification!