473,465 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Do "generic" objects know what types they contain?

Newbie question, hopefully not one that will embarrass me.

I'm reading Richter's Applied MS .NET Framework Programming and keep
seeing snippets such as this:

Int32 v = 5;
Object o = v;
Console.Writeline("{0}, {1}, {2}", o, o, o);

in which it appears that code referencing an Object magically knows
the type of data it contains. Is the type stored in the object
itself, or is it determined by some set of rules?
Nov 16 '05 #1
3 1662
"Tom H." <ne********@yahoo.com> wrote:
Int32 v = 5;
Object o = v;
Console.Writeline("{0}, {1}, {2}", o, o, o);
[...]
Is the type stored in the object itself, or is it determined
by some set of rules?


Int32 inherits from Object, so its overridden version of ToString is
used for output.

P.
Nov 16 '05 #2
All objects in C# inherit from the base class System.Object. System.Object
provides a GetType() method so you can always find out exactly what type of
object is being held by a System.Object.
System.Object also provides a ToString() method which is what is being
called by WriteLine, but since the object is really an Int32,
Int32.ToString() is called instead of Object.ToString().

"Tom H." <ne********@yahoo.com> wrote in message
news:f3**************************@posting.google.c om...
Newbie question, hopefully not one that will embarrass me.

I'm reading Richter's Applied MS .NET Framework Programming and keep
seeing snippets such as this:

Int32 v = 5;
Object o = v;
Console.Writeline("{0}, {1}, {2}", o, o, o);

in which it appears that code referencing an Object magically knows
the type of data it contains. Is the type stored in the object
itself, or is it determined by some set of rules?

Nov 16 '05 #3
Yes each object knows its actual type:

Int32 v = 5;
Object o = v;
Console.Writeline(o.GetType());

Will print System.Int32. This is because '5' is converted into a regular
object before it is stored into o.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"Tom H." <ne********@yahoo.com> schrieb im Newsbeitrag
news:f3**************************@posting.google.c om...
Newbie question, hopefully not one that will embarrass me.

I'm reading Richter's Applied MS .NET Framework Programming and keep
seeing snippets such as this:

Int32 v = 5;
Object o = v;
Console.Writeline("{0}, {1}, {2}", o, o, o);

in which it appears that code referencing an Object magically knows
the type of data it contains. Is the type stored in the object
itself, or is it determined by some set of rules?

Nov 16 '05 #4

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

Similar topics

1
by: Vicente Nicolau | last post by:
hello, I have a problem when I preview or print a document in an ISO A0. I have an application which load an 10x10 image (bmp or jpg) and spread it in the A0 paper. When I make the print...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
0
by: Prasana | last post by:
Hi EveryBody, I have one Problem When I am Creating the DNS Zone Using Resource Record using System Management. Iam Getting an Error Called "Generic Error". Iam Able to Create a Primary Zone File...
1
by: jason | last post by:
Hello everyone, I'm trying to expand my understanding of the DataGrid control, and I'm specifically curious about the Command columns. I have successfully implemented an EditCommandColumn, but I...
4
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172:...
5
by: Torben Laursen | last post by:
I am writing a COM in C# using visual studio 2005 and VSTO. Inside the code I use some support classes that are generic but they are not used in the inferface of the COM. However I still get a...
0
by: epoxyparser | last post by:
Hi all, I work as a glue coder and often have to interface existing mechanisms with other platforms. Creating bridge code between command-line functionality and desktop GUIs takes up a lot of that...
2
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to...
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
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...
1
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...
0
tracyyun
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...
0
isladogs
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...

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.