Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 8 of 8

Thread: Some c# questions :)

  1. #1

    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
    Thanks!

  2. #2
    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.
    These are some great questions. I'll answer the first one in this post.

    Let's say you have Class A and Class B, and they both implement the same method called getName(). If B inherits from A, then if you were to call the getName() method on an object of type B, which getName would be called? Would the one in A be called, or will the one in B be called? Since your object is of type B, only B's getName() method will be called.

    You can clarify by explicitly using the new keyword in the extended getName() method in B. In C#, your application will behave as expected if you forget to include the new keyword. You actually only receive a compiler warning!

    For virtual, override, and polymorphism, the following example should help you out:
    Code:
    class Foo
    {
        public virtual void getName()
        {
            Console.WriteLine("Foo!");
        }
    }
     
    class Zorb : Foo
    {
        public override void getName()
        {
            Console.WriteLine("Zorb!");
        }
    }
     
    class Program
    {
        static void Main(string[] args)
        {
            // Simple foo declaration
            Foo foo = new Foo();
            foo.getName();
     
            // Polymorphism?
            Foo foo2 = new Zorb();
            foo2.getName();
     
            //Output is:
            // Foo!
            // Zorb!
        }
    }
    Try messing around with the above code and see which produces the answer you want. Only by overwriting Foo's virtual getName method, will I be able to print out

    Zorb! In the second example where Foo foo2 = new Zorb(), if I don't declare getName() as having been overridden in Zorb, the output would actually be Foo! Even using the new keyword instead of override will result in Foo! being displayed.

    If you have any questions, feel free to reply back.

    Cheers!
    Kirupa

    Great, now even Kirupa is { facebooking | twittering }

  3. #3
    Hi Kirupa,

    Thanks for your response

    I think I understand you. So using the 'new' keyword just tells the compiler explicitly which method you wish to use if a derived class uses the same method as a base class, whereas using the 'virtual' and 'override' keywords allows you to use polymorphism.

    One thing that slightly bugs me though, is that even though I know this is the case (and could therefore write code that would take that into account), I am curious as to the actual underlying mechanics that make these distinctions important.

    I guess I just don't quite understand why the new keyword for example would create a problem in terms of inheritence, whereas the virtual and override keywords do not.

    I'm guessing that when you do inheritence, the object is somehow 'flattened' at run time, in that maybe the distinction between a base class and the derived class is lost... If this were the case I think it might make sense, as I could understand for example that the override keyword would say to the compiler "use this method as the ONE method to be used with this name for this particular class" whereas the new keyword might create a conflict, with the compiler not knowing what to do. Please bare in mind that I have absolutely no idea whether I'm right about this. I guess I'm just interested in the mechanics by which inheritence and polymorphism works, so I know what's going on underneath the surface.

    There are a few other things I'd like to ask at some point if that's ok. Thanks for responding though Kirupa - I appreciate it

    Just for the record, I'm currently working in a job (non it related, but that's another story) where virtually all it websites are blocked by the firewall. For some reason, your site isn't blocked, so I've been reading through lots of your articles (At least I feel like I'm doing SOMETHING productive in this place). Just wanted to say you've got a nice site, and I like your presentation and the way your articles are written.

    I guess the questions I'm asking here could come in handy for other c# n00bs too

    Thanks

    aaarrrggh

  4. #4
    A lot of optimizations are done behind the scenes when a .NET app is run, so your explanation about a reason why one might need to explicitly specify new/override/virtual might be the correct answer. I really don't know for sure though

  5. #5
    Ah right ok. Thanks for the advice Kirupa

  6. #6
    I'm guessing that when you do inheritence, the object is somehow 'flattened' at run time

    Nope The object isn't flattened. Each object has a table of function pointers which are used to lookup the correct function to call when virtual functions are called. No function pointer references are lost, otherwise you wouldn't be able to call the base class's version of the virtual function from inside the derived class.

    A function that is declared as 'new' will shadow an existing function with the same signature in the base class. This is not the same as a virtual function. Check out the following code:
    Code:
    class X
    {
        public void Func()
        {
            // ...
        }
        
        public virtual void Func2()
        {
            // ...
        }
    }
    
    class Y : X
    {
        public new void Func()
        {
            // ...
        }
    
        public override void Func2()
        {
            // ...
        }
    }
    
    X x = new X();
    Y y = new Y();
    X a = x;
    X b = y;
    
    x.Func(); // calls Func() in X
    y.Func(); // calls Func() in Y
    x.Func2(); // calls Func2() in X
    y.Func2(); // calls Func2() in Y
    
    a.Func(); // calls Func() in X
    b.Func(); // calls Func() in Y
    a.Func2(); // calls Func2() in X
    b.Func2(); // calls Func2() in Y
    I hope that helps clear it up

    Cheers!
    OJ
    Last edited by TheColonial; August 13th, 2007 at 09:53 PM. Reason: Error spotted by spider.

  7. #7
    not
    a.Func2(); // calls Func2() in Y
    but
    a.Func2(); // calls Func2() in X

  8. #8
    Oops.. yup. Sorry, typo Fixed now. Cheers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012