473,385 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Class Object and override

Hello!!

You may correct me if I have made any wrong assumptions.

Below I have some simple classes.
When you have this t.ToString() below it's the ToString() method in Object
class that is called
and when you have this t.GetType() below it's the GetType() method in the
Object class that is called.

When you have 123.ToString() it's the ToString method in the string class
that is called. When you have 123.GetType() it's the GetType that is called
in the string class. Is this right understood.
One more question.
I just wonder how can the Object class know what the class is because you
just call a method
ToString or GetType in the Object class. For example when you do
t.ToString() or t.GetType()how can the Object class know that the Test
should be written.
class Test
{}

class myApp
{
public static void Main()
{
Test t = new Test();
Console.WriteLine("ToString: {0}", t.ToString() );
Console.WriteLine("Type: {0}",t.GetType() );

Console.WriteLine("ToString: {0}", 123ToString() );
Console.WriteLine("Type: {0}", 123.GetType() );
}

//Tony


May 31 '06 #1
3 1707
Hello!

Note:
I made a typo in my example program below it should be Object t = new
Test(); and not what is written now Test t = new Test();

//Tony

"Tony Johansson" <jo*****************@telia.com> skrev i meddelandet
news:qu****************@newsb.telia.net...
Hello!!

You may correct me if I have made any wrong assumptions.

Below I have some simple classes.
When you have this t.ToString() below it's the ToString() method in Object class that is called
and when you have this t.GetType() below it's the GetType() method in the Object class that is called.

When you have 123.ToString() it's the ToString method in the string class that is called. When you have 123.GetType() it's the GetType that is called in the string class. Is this right understood.
One more question.
I just wonder how can the Object class know what the class is because you just call a method
ToString or GetType in the Object class. For example when you do
t.ToString() or t.GetType()how can the Object class know that the Test
should be written.
class Test
{}

class myApp
{
public static void Main()
{
Test t = new Test();
Console.WriteLine("ToString: {0}", t.ToString() );
Console.WriteLine("Type: {0}",t.GetType() );

Console.WriteLine("ToString: {0}", 123ToString() );
Console.WriteLine("Type: {0}", 123.GetType() );
}

//Tony




May 31 '06 #2
Actually, no.

Assuming that the Test class has a ToString() method overriding the base
class method, it is the Test class ToString() method that will be called
becuase the object referenced by t is actually of type Test even thought is
of type object. The same goes for the GetType() method.

123.ToString() and 123.GetType() would call the methods of the integer class
because 123 is an integer.

--
Jeffrey Hornby
Hornby Consulting, Inc.

"Tony Johansson" wrote:
Hello!

Note:
I made a typo in my example program below it should be Object t = new
Test(); and not what is written now Test t = new Test();

//Tony

"Tony Johansson" <jo*****************@telia.com> skrev i meddelandet
news:qu****************@newsb.telia.net...
> Hello!!
>
> You may correct me if I have made any wrong assumptions.
>
> Below I have some simple classes.
> When you have this t.ToString() below it's the ToString() method in

Object
> class that is called
> and when you have this t.GetType() below it's the GetType() method in

the
> Object class that is called.
>
> When you have 123.ToString() it's the ToString method in the string

class
> that is called. When you have 123.GetType() it's the GetType that is

called
> in the string class. Is this right understood.
>
>
> One more question.
> I just wonder how can the Object class know what the class is because

you
> just call a method
> ToString or GetType in the Object class. For example when you do
> t.ToString() or t.GetType()how can the Object class know that the Test
> should be written.
>
>
> class Test
> {}
>
> class myApp
> {
> public static void Main()
> {
> Test t = new Test();
> Console.WriteLine("ToString: {0}", t.ToString() );
> Console.WriteLine("Type: {0}",t.GetType() );
>
> Console.WriteLine("ToString: {0}", 123ToString() );
> Console.WriteLine("Type: {0}", 123.GetType() );
> }
>
> //Tony
>
>
>
>
>



May 31 '06 #3

"Tony Johansson" wrote...
You may correct me if I have made any wrong assumptions.
Yes, you have... ;-)
Below I have some simple classes.
When you have this t.ToString() below it's the
ToString() method in Object class that is called and when you have this
t.GetType()
below it's the GetType() method in the Object class that is called.
Yes, so far so good...
When you have 123.ToString() it's the ToString method in the string class
that is called. When you have 123.GetType() it's the GetType that is
called in the string class. Is this right understood.
Nope, 123 is a literal of type int (System.Int32), which means that the
ToString-method defined in the class System.Int32 that will be called.

As System.Int32 haven't overridden the GetType-method, that is still the
method defined in the System.Object class that will be executed.
One more question.
I just wonder how can the Object class know what the
class is because you just call a method ToString or
GetType in the Object class.
Even if the methods aren't overridden, they nevertheless "belong" to the
actual type of the instance anyway, through inheritance.

That means in simple terms that when you call GetType on an instance of
Int32, you call it on an instance of Int32, not an instance of Object.
For example when you do t.ToString() or t.GetType()how can the Object
class
know that the Test should be written.


Each instance "knows" of what actual type it is itself.

When you call GetType on t, it uses the implementation inherited from
Object, retrieving the runtime type internally, returning that as an
instance of the class Type.

As you then use it in the WriteLine-method, that will in turn invoke the
ToString method from the Type class, in order to get a string representation
it can print.

When you call ToString on t, as it doesn't override ToString, it uses the
default implementation of ToString inherited from Object, which actually
calls GetType().FullName in order to get a "generic" string representation
of the instance.

When you call 123.ToString(), that call makes use of the overriding
implementation of ToString in System.Int32, which involves the use of
NumberFormatters, etc...
HTH.

// Bjorn A


May 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: bengamin | last post by:
Hi, I have a C# class and two instance of the class; the class have some property. I want to compare the property value of the two instance How should i do? override == ? use delegate ?
6
by: Brad | last post by:
I am creating a class (not a control) which implements IStateManager. I've created the class and all of the implementations (LoadView, SaveViewState, etc...). My Question is: How do I...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
3
by: polocar | last post by:
Hi, I'm writing a C# program (using Visual Studio 2005 Professional Edition). I have defined a class MyPanel in the following way: class MyPanel : Panel { ... }
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
1
by: archana | last post by:
Hi all, I am confuse in concept of inheritance. I am having following 2 classes class base1 { public void abc() { System.Console.WriteLine("base1 abc"); }
1
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.