Some c# questions :)

Hello there. This is my first post on this forum. You lot seem like a friendly bunch, so I
thought I’d have a go at asking you a few questions about c#, if that’s ok

Just for a bit of context - I’ve been teaching myself using online tutorials and a few
books, but there are a few things that I seem to be having a little trouble understanding.
In some places I’m in a position where I think I could actually write code that would work,
but I’m frustrated in that I feel like I don’t fully understand what is going on behind the
scenes.

I’m currently reading the book “Learning c#, 2nd edition”, by O’Reilly. For the most part
it’s been useful, but there are some things I’m unclear on.

Here’s a few:

Inheritence

When inheriting from a base class, I don’t fully understand the difference between using
virtual methods and the override keyword, and in using the ‘new’ keyword to ‘hide’ the base
method. The book uses the term ‘hide’, but I find it unclear as to what this means.
I understand that polymorphism only works using virtual methods and the override keyword,
but I don’t quite understand why this is.

There’s something that’s always bugged me too. Might seem like a silly question to a more
experienced user, but seeing as I don’t know the answer, I thought I’d ask it anyway.

I (kinda) understand the differences between value types and reference types. I understand
for example, that if you do this:

Dog Milo = new Dog();
Milo.weight = 5;

Dog Fido = Milo();
Fido.Weight = 10;

That Milo.Weight will now = 10, as it’s only actually referencing the object, not storing
the value of the object itself. However, there’s still one or two things I don’t quite
understand.

One is this: why do value types still have methods and properties? I know they inherit from
Object, but why then do they remain value types? If int inherits from Object, why is it not
a reference type? I’ve got a feeling this is to do with boxing, but I didn’t fully
understand boxing to be honest…

One final thing for now - looking at the example above, I wanted to ask something else.

If we split this line: Dog Milo = new Dog(); into two seperate lines for a moment (and
ignoring the fact that this might cause a compiler error or whatever), can anybody tell me
if I’m right in thinking that this:

Dog Milo
creates a reference in itself? I was thinking, if I’m right, that the ‘Dog Milo’ part creates a reference object (I hope I’m right here), and that the ‘new Dog();’ element then creates the dog object, which is then referenced by Milo. The reason I’m asking this is because I got really confused by the section on interfaces in the book I’m reading, particularly when it showed you how to cast to an interface.

Anyway, I have more questions, but thought I’d ask these first. I’d really appreciate it if anybody could help me. I know I’ve practically written an essay here, but these are things that I’m finding frustrating, as it seems hard to find a direct explanation for some of these things.
Hope some of you guys can help :slight_smile:
Thanks!