Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 26th, 2006, 08:51 PM
Authorized User
 
Join Date: Sep 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to guntank
Default Behavior of "this" & "base" in inheritance

First case:
    class Parent
    {
        public void Me()
        {
            Console.WriteLine(this);
        }
    }
    class Child : Parent
    {

    }
    class Program
    {
        static void Main(string[] args)
        {
            Child child = new Child();
            child.Me();
        }
    }
The output is "Child" rather than "Parent". Can you explain what is the concept behind it?



Second case:
    class Parent
    {
        public void Me()
        {
            Console.WriteLine(base.ToString());
        }
    }
    class Child : Parent
    {

    }
    class Program
    {
        static void Main(string[] args)
        {
            Child child = new Child();
            child.Me();
        }
    }
The output is still "Child" rather than "Parent". Can you explain what is the concept behind it?


 
Old September 26th, 2006, 10:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Here is the IL for your first case:

Code:
.method public hidebysig instance void  Me() cil managed
{
  // Code size       7 (0x7)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  call       void [mscorlib]System.Console::WriteLine(object)
  IL_0006:  ret
} // end of method Parent::Me
The overlaod of the WriteLine method that takes an object as a parameter is called. The ToString method of the object type (Child) is called to return a string representation of the object. Keyword ‘this’ refers to an instance of the Child class, which simply inherits (and invokes) a member implementation defined in its base class, Parent. It is not the case that an object of type Parent is invoking the Me method.

Here is the IL for your second case:

Code:
.method public hidebysig instance void  Me() cil managed
{
  // Code size       12 (0xc)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  call       instance string [mscorlib]System.Object::ToString()
  IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000b:  ret
} // end of method Parent::Me
Here the Child class calls its base class’s implementation of the ToString method (as indicated by keyword 'base'). However, the parameterless ToString method is defined in System.Object (the Parent class’s direct base class). So the Child class is ultimately using System.Objects implementation of ToString which simply returns a string representation of the object, hence Child.

Child inherits the member implementations defined in the Parent class. When those implementations are invoked in your code, however, they are invoked by an object of type Child, not an object of type Parent.

Anyway, that's my story, and I'm sticking to it...for now...

HTH,

Bob


 
Old September 26th, 2006, 11:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Should mention that in the first case the ToString method defined in System.Object is also used when Child.ToSting() is called by the System.Console.WriteLine method. Child objects derive from System.Object also, as do all reference types.

So in both cases, System.Object's definition of ToString is simply returning a string representation of a Child object. Your code just invokes System.Object.ToSting() in two different ways.

Bob

 
Old September 26th, 2006, 11:29 PM
Authorized User
 
Join Date: Sep 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to guntank
Default

Thanks for your reply. I can understand your well explanation.
If I want to print out the fully qualified name of the Parent class from the Child class, how to do it?
Is it possible to obtain it from the "base" keyword?
 
Old September 27th, 2006, 06:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

The BaseType property of the Type class will return the type from which the current type directly inherits. Here's a DisplayBaseType method for your Child class:

Code:
class Parent {
    public void Me() {
        Console.WriteLine(base.ToString());
    }
}
class Child : Parent {

    public void DisplayBaseType(){

        Type t = this.GetType();
        Console.WriteLine("{0} inherits from {1}.", this.ToString(), t.BaseType);


    }

}
class Program {
    static void Main(string[] args) {
        Child child = new Child();
        child.Me();
        child.DisplayBaseType(); 
 
        Console.Read();
    }
}

HTH,

Bob
 
Old September 27th, 2006, 09:24 AM
Authorized User
 
Join Date: Sep 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to guntank
Default

Thanks Bob for your reply.
In C++ we can declare an alias of a varible using the following

AnyType var1(...);
AnyType& var2=var1;

or using the same mechanism in function parameter as follows

void Foo(AnyType& arg)
{
...
}


In C# we can do the second case above as follows

void Foo(ref AnyType arg)
{
...
}

Even though the first case seems useless in practical sense,
how to do it (the first case) in C#? Is it possible?


Andi Setiawan

 
Old September 27th, 2006, 07:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Andi,

Not quite sure this is what you're looking for. If not, its always a good idea to post a new question on a new thread. Better chance of someone tossing the the right solution out there.

using System;

class RefType {
    public int i;
    public double d;
    public RefType(int i, double d) {
        this.i = i;
        this.d = d;
    }
    public override string ToString() {
        return "(" + i + ", " + d + ")";
    }
}

public class App {
    public static void Main() {

        RefType r1;
        r1 = new RefType(1, 1.1);
        Console.WriteLine("r1 = " + r1);

        RefType r2;
        r2 = r1;

        // r2 and r1 now contain the same memory address of a
        // single object on the managed heap. So any operation
        // on one variable will affect the object referenced by
        // the other variable.

        r2.i++; r2.d++;
        Console.WriteLine("r1 = " + r1);
    }
}


HTH,

Bob


 
Old September 27th, 2006, 09:00 PM
Authorized User
 
Join Date: Sep 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to guntank
Default

Thanks for your reply.
I am sorry, it is not what I mean.

You are right that both r1 and r2 refer to the same object in managed heap.
But r1 is not an ALIAS of r2. It means that if we change r1 to refer to a new object, now both r1 and r2 refer to distinct objects.

In C++, if r1 is an alias of r2 then r1's address must be equal to r2's address (&r1 == &r2 must return true).

When we use ref keyword in method's parameter in C#, it works like C++ alias.
In C++ we can declare aliases both in method's body and method's parameters.
Thus, I just want to know whether or not C# support alias in method's body.

Andi Setiawan





Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGridView Behavior Qs BrianWren Pro Visual Basic 2005 0 March 1st, 2007 03:49 PM
Questions about Inheritance & Polymorphism jimblanc Java Basics 1 December 31st, 2006 03:54 PM
Javascript && keeps turnig into && ayrton Pro VB.NET 2002/2003 3 June 27th, 2005 03:34 PM
Linux & KDE & C++ & QT & MYSQL & Kdevelop Munnnki Linux 0 January 2nd, 2005 05:41 PM
base class inheritance krunch97 VB.NET 2002/2003 Basics 6 April 23rd, 2004 11:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.