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.WriteLi ne("ToString: {0}", t.ToString() );
Console.WriteLi ne("Type: {0}",t.GetType( ) );
Console.WriteLi ne("ToString: {0}", 123ToString() );
Console.WriteLi ne("Type: {0}", 123.GetType() );
}
//Tony 3 1719
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.WriteLi ne("ToString: {0}", t.ToString() ); Console.WriteLi ne("Type: {0}",t.GetType( ) );
Console.WriteLi ne("ToString: {0}", 123ToString() ); Console.WriteLi ne("Type: {0}", 123.GetType() ); }
//Tony
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.WriteLi ne("ToString: {0}", t.ToString() ); > Console.WriteLi ne("Type: {0}",t.GetType( ) ); > > Console.WriteLi ne("ToString: {0}", 123ToString() ); > Console.WriteLi ne("Type: {0}", 123.GetType() ); > } > > //Tony > > > > >
"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().FullN ame 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
NumberFormatter s, etc...
HTH.
// Bjorn A This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ?
|
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 instantiate this class in a page and
then re-instantiate it on post back such that I don't lose it's viewstate?
If the page always uses x = new myclass to...
|
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 and/or processes so
that multiple threads/processes can write to the same file (all threads
use the same instance of the stream, processes use a...
|
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
{
...
}
|
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 magic, I
wrote the following module. It is mainly useful as a light weight tool
to help programmers catch mistakes at definition time (e.g.,...
| |
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");
}
|
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 the product
source code, but still want to execute them under nunit.
I am trying this idea on nunit sample source code. Here is my class
and the...
|
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 tells me if a variable has
changed, give me the original and current value, and whether the current
value and original value is/was null or not.
...
|
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 class is used as a function
that keeps state from one call to the next.
The problem is that I don't know what to call such a thing! "Abstract
class"...
|
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 the app is
concerned). The problem is, If someone makes a typo, they may get an
unexpected error due accidentally calling the original attribute...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |