Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

instantiation; and arrays & structures really objects?

Status
Not open for further replies.

thedougster

Programmer
Jan 22, 2009
56
0
0
US
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]:
Code:
ClassName ObjectName;
(Am I not correct that a handle is simply an address?)

To instantiate an object (allocate space for it in the heap):
Code:
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:
Code:
ObjectName = new ClassName ();
To declare an object (variable), instantiate an object and assign the object to the object variable within a single statement:
Code:
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):
Code:
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:
Code:
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?)
 
I apologize for answering your post with a link (especially considering that you proclaim to have spent some time with your head in the documentation already), however if I were to respond directly to your questions my answer would be close to what this article says.... If you haven't already read this one, check it out...


Please respond back with any specific questions pertaining to the article. I will be happy to try to shed more light on it for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top