Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 01-21-2009, 03:49 PM   #1
Baseband Member
Join Date: Nov 2008
Posts: 91
Default instantiation; and arrays & structures really objects?

Pardon me for flogging a dead horse here, but I'm trying to understand as clearly as possible some issues pertaining to the instantiation of objects.

Firstly, have I correctly analyzed the functions and uses of each of the various syntaxes related to instantiating objects? Please don't flame me, saying "why don't I read the documentation". I've read it thoroughly. The point is I want to understand the material in finer detail than for which I can find documentation.

To declare an object [create an object variable to hold a reference ("handle") of an object of the required class type]:
ClassName ObjectName;
(Am I not correct that a handle is simply an address?)

To instantiate an object (allocate space for it in the heap):
new ClassName ();
When an object is instantiated, it is usually within the same statement "assigned" to an object variable (see immediately below). This "assignment" means the reference of the object is placed into the object variable to make the variable "point to" the object. However, this reference may instead be returned to a calling method without assigning it to an object variable.

To instantiate an object and assign it to an object variable within a single statement:
ObjectName = new ClassName ();
To declare an object (variable), instantiate an object and assign the object to the object variable within a single statement:
ClassName ObjectName = new ClassName ();
To assign the reference of an object held by one object variable to another object variable (to make both object variables point to the same object):
ObjectName2 = ObjectName;
Secondly, what is the precise relationship of arrays and structures to classes and objects?

Objects are instantiations of classes, and as such are reference-type variables, space for which is allocated in the heap, not on the stack.

struct is said to be a "lightweight" form of class, and structures, like objects, must be instantiated. But structures can be instantiated without use of the keyword "new" (unlike classes), which means that no constructor is executed when structures are instantiated in this way.

Also, structures are of value-type, and so space for them must be allocated on the stack, not in the heap. Plus, like all value types, structures lack inheritance functionality.

So are structures objects or not?

I have read that arrays ARE objects. They also can be instantiated without use of the keyword "new", but only if they are also initialized (and sized) within the same statement:
DataType [] ArrayName = { < element list > };
In this case, it must be that, as with structures, no constructor is executed.

In addition, although the individual elements of an array may be of a user-defined data type, an array itself is never an instantiation of a user-defined data type (I think). Furthermore, like structures, arrays lack inheritance functionality.

So are arrays really objects? They seem so different from other objects.

And just as a matter of curiosity:

Arrays are inherited from the System.Array class. But ALL data types in C#, even value types including implicit types, are implicitly inherited from the System.Object class. (Of course, that doesn't mean all variables, regardless of data type, are objects.)

So is the System.Array class inherited directly from the System.Object class? (For that matter, is there any way I can actually read the namespace files?)

BobLewiston is offline   Reply With Quote
Old 01-22-2009, 10:14 AM   #2
In Runtime
Daeva's Avatar
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: instantiation; and arrays & structures really objects?

First of all, we do not (at least we try not) to participate in flaming.
Secondly, when you research a topic, have a basic understanding of it, and are able to articulate a question while giving all relevant information related to the question, you are not likely to get flamed. In fact, you're more likely to not have any responses because you are fully qualifying the people who would respond and eliminating guesswork.

As for your question, your understanding of the instantiation of object as well as knowledge related to how they are stored in memory is not something people who work in higher level languages (like C# or VB) have a firm grasp on. I would say you are dead on.

As for whether or not structures are Objects, it depends on how stringent your definition of "object" is. If you are referring to them in a general OOP sense of the word, no. Structures differ from classes in several ways, chiefly among these is it's lack of inheritance. Structures are one of the lowest level elements in a C# or VB.Net environment, and as such you cannot have anyting "lower" (inheritance from a structure), and the structure cannot be dependent upon anything "higher" (the structure inheriting from another object).
If you are referring to them in a specific class/inheritence structure, yes, because like you stated, all classes inherit from System.Object.

Arrays are in fact objects. The part where the line begins to blur is what exactly is held in your array. It could actually hold value-types or provide references to object-types, depending on it's initialization.

Part of the misconception I think arises from the difference in array types in c++ (static/dynamic). Using a static array in c++ means that it is stored on the stack, not the heap, which is incredibly inflexible. Dynamic arrays in c++ however are stored on the heap, and can be re-sized.
Though in C# and vb.net arrays (in their base form) must also have (without the new keyword) a defined size. Despite this fact, they are still technically considered reference-types.

You made me pick my brain on this one , I hope I've been able to clear up any questions you had.

I've never thought extensively about this subject, nice question.

**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
Daeva is offline   Reply With Quote
Old 01-22-2009, 12:40 PM   #3
Baseband Member
Join Date: Nov 2008
Posts: 91
Default attention, Daeva:

Thanks for your answer. I'm sure I'll pick your brain from time to time, but right now I have no questions.

As for my comments about flaming, my experience in life is that reasonable, polite behaviour isn't always a guarantee against rudeness. It seems there's often enough someone who's prone to verbal aggression, especially when they can remain anonymous. Of course, it can be rare in some environments, such as (I have no reason to doubt) this forum.
BobLewiston is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 05:37 PM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0