base vs this

R

Raj

Both this.ToString() and base.ToString() returns the same in derived class. I
assumed base will point to base class and this will point to invoking
object's class.

Ex:
class iambaseclass
{
public int var1;
}

class iamchildclass:iambaseclass
{
public int var2;
Console.WriteLine(base.ToString());
Console.WriteLine(this.ToString());
}

When I type base. intellisense it displays var1
When I type this., intellisense displays both var1, and var2

Very much confused ... intellisense and the programs output is contradictory!

Pl. justify

Thank you

Regards
Raj
 
M

Morten Wennevik [C# MVP]

Hi Raj,

The default implementation of ToString is as follows

public virtual string ToString()
{
return this.GetType().ToString();
}

Even though you are calling base.ToString() you are in effect just calling
this.ToString(). As for why base.GetType == this.GetType is a little more
confusing.

From the C# specs
"At compile-time, base-access expressions of the form base.I and base[E] are
evaluated exactly as if they were written ((B)this).I and ((B)this)[E]"

So even though you write base.GetType() it will compile to
((iambaseclass)this).GetType() which will return iamchildclass since

iambaseclass i = new iamderivedclass();
i.GetType() == iamderivedclass

As for why only var 1 is visible in base, consider the previous code. base
effectively casts the reference to its base type and the base type is not
able to see var2.

so to sum up

base.ToString() == ((iambaseclass)this).ToString() =>
this.GetType().ToString()
base.GetType() == ((iambaseclass)this).GetType() => this.GetType()
base.var2 == ((iambaseclass)this).var2 => compiler error, iambaseclass does
not see var2.

On the other hand, if you override ToString to return different results you
will see the difference between base.ToString() and this.ToString()
 
K

Konrad Neitzel

Hi Raj,

I am not sure, if I got you right, but I try to answer you.

The Intellisense is just looking up, what it knows about the classes to
help you type stuff quickly and avoid typographic errors.

"this" refers to the instance of the class. So your child class has the
members of the base class and it's own members (So var1 and var2 are
shown. Var1 is public so it is visible from the child.)

"base" refers to the base only. So it can only show you the members of
the base and does not show you the members of the class itself. (Only
var1, because that is the public member. Var2 is part of the derivered
class so it is not shown as part of base!)

The function ToString() is a simple function that comes with
System.Object. This Object does not know anything about your Objects so
it simply gives some default output.

So in both of your classes, the same ToString() function is called.

See http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx
for more information.

I hope I was able to help you.

With kind regards,

Konrad
 
M

Morten Wennevik [C# MVP]

Just to avoid some potential confustion, when you call base. virtual method
it will not be resolved to (baseclass)this since this would prevent the
ability to call the base method, and indeed if you override a method in
visual studio the default implementation is to call base.Method()

--
Happy Coding!
Morten Wennevik [C# MVP]


Morten Wennevik said:
Hi Raj,

The default implementation of ToString is as follows

public virtual string ToString()
{
return this.GetType().ToString();
}

Even though you are calling base.ToString() you are in effect just calling
this.ToString(). As for why base.GetType == this.GetType is a little more
confusing.

From the C# specs
"At compile-time, base-access expressions of the form base.I and base[E] are
evaluated exactly as if they were written ((B)this).I and ((B)this)[E]"

So even though you write base.GetType() it will compile to
((iambaseclass)this).GetType() which will return iamchildclass since

iambaseclass i = new iamderivedclass();
i.GetType() == iamderivedclass

As for why only var 1 is visible in base, consider the previous code. base
effectively casts the reference to its base type and the base type is not
able to see var2.

so to sum up

base.ToString() == ((iambaseclass)this).ToString() =>
this.GetType().ToString()
base.GetType() == ((iambaseclass)this).GetType() => this.GetType()
base.var2 == ((iambaseclass)this).var2 => compiler error, iambaseclass does
not see var2.

On the other hand, if you override ToString to return different results you
will see the difference between base.ToString() and this.ToString()

--
Happy Coding!
Morten Wennevik [C# MVP]


Raj said:
Both this.ToString() and base.ToString() returns the same in derived class. I
assumed base will point to base class and this will point to invoking
object's class.

Ex:
class iambaseclass
{
public int var1;
}

class iamchildclass:iambaseclass
{
public int var2;
Console.WriteLine(base.ToString());
Console.WriteLine(this.ToString());
}

When I type base. intellisense it displays var1
When I type this., intellisense displays both var1, and var2

Very much confused ... intellisense and the programs output is contradictory!

Pl. justify

Thank you

Regards
Raj
 
G

Gregory A. Beamer

Both this.ToString() and base.ToString() returns the same in derived
class. I assumed base will point to base class and this will point to
invoking object's class.

The default ToString(), not overridden, is to call base anyway, as you
have not implemented ToString(). If you use the default implementation
of ToString() in many class types, it simply calls base.ToString(). For
example, I just added the ToString() and I get this:

public override string ToString()
{
return base.ToString();
}

Until you change the implementation of ToString(), you will get the same
answer.

Peace and Grace,


--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
R

Raj

Still no respite!

Thanks

Regards
Raj

Gregory A. Beamer said:
The default ToString(), not overridden, is to call base anyway, as you
have not implemented ToString(). If you use the default implementation
of ToString() in many class types, it simply calls base.ToString(). For
example, I just added the ToString() and I get this:

public override string ToString()
{
return base.ToString();
}

Until you change the implementation of ToString(), you will get the same
answer.

Peace and Grace,


--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
.
 
P

Peter Duniho

Raj said:
Still no respite!

What does that mean? I mean, in English that would generally mean "I am
still suffering!" But Gergory's reply gave what I believe, given your
original question, is exactly the information you need.

Why are you still suffering?

Pete
 
H

Harlan Messinger

Raj said:
Both this.ToString() and base.ToString() returns the same in derived class. I
assumed base will point to base class and this will point to invoking
object's class.

Ex:
class iambaseclass
{
public int var1;
}

class iamchildclass:iambaseclass
{
public int var2;
Console.WriteLine(base.ToString());
Console.WriteLine(this.ToString());
}

This won't compile, so you aren't showing us actual, working code.
When I type base. intellisense it displays var1
When I type this., intellisense displays both var1, and var2

Very much confused ... intellisense and the programs output is contradictory!

It's different, but it isn't contradictory, because you're talking about
two different things. If your code were like this:

class iambaseclass
{
public int var1;
}

class iamchildclass:iambaseclass
{
public int var2;
public void Test()
{
Console.WriteLine(base.ToString());
Console.WriteLine(this.ToString());
}
}

class Program
{
static void Main(string[] args)
{
iamchildclass c = new iamchildclass();
c.Test();
Console.ReadKey();
}
}

Intellisense shows the possibilities available for the type of the
*expression* before the period. The type of an iamchildclass's base is
iambaseclass. Therefore, "base." causes choices for imabaseclass to
appear. It wouldn't show *var2* as an option for "base." because
*base.var2* wouldn't be a valid expression.

Unless it has been overridden, ToString is defined to show the name of
the type of the *object* to which the expression *refers*. It has to:
the object's ToString method has no way to know what type of reference
you used to access the method. The object is the same object, having
type iamchildclass, whether you reference it through *this* or through
*base*.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top