Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

instantiation; and arrays & structures really objects?


  • Please log in to reply
4 replies to this topic

#1 BobLewiston

BobLewiston

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:05:10 AM

Posted 21 January 2009 - 04:46 PM

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?)

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:10 AM

Posted 21 January 2009 - 06:12 PM

Let's see how many of these I can answer, then we'll have a game to see how many I answered correctly. :thumbsup: In no particular order:

So are structures objects or not?

In object oriented programming, everything is an object, even arrays. What about arrays seems so different than any other data types?

They also can be instantiated without use of the keyword "new", but only if they are also initialized (and sized) within the same statement:

How can you size a newly instantiated array without using new?

Am I not correct that a handle is simply an address?

Usually not. In most cases, it is a pointer to an address. I think perhaps you are actually talking about a variable here because a handle has a different definition.

Now to the instantiation, initialization, and declaration stuff. A declaration (according to all of my programming books) is simply something like double d. Initialization is assigning a value, ie. d=20.0. As you pointed out, the declaration and instantiation can be done in the same statement. double d = 20.0.

Instantiation occurs when you create an instance of an instantiable class. So by declaring an object and initializing it, you are creating an instance. That is how I understand it. And there do exists classes that can not be instantiated. In Java, interfaces and abstract classes can not be directly instantiated.

Did we clear up a few of these questions?

#3 BobLewiston

BobLewiston
  • Topic Starter

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:05:10 AM

Posted 21 January 2009 - 06:45 PM

attention, groovicus:
>>So are structures objects or not?

>In object oriented programming, everything is an object, even arrays.

Is EVERYTHING an object, even implicit data types like int?

>What about arrays seems so different than any other data types?

I guess I got confused by the fact that the individual elements are usually implicit types. Of course, that doesn't mean that the array itself is so greatly different than other 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:

>How can you size a newly instantiated array without using new?

Well, when you code:
DataType [] ArrayName = { < element list > };
the number of elements in your element list sets the size of the array.

>>Am I not correct that a handle is simply an address?

>In most cases, it is a pointer to an address.

Acknowledged. And so a handle is not a reference; rather, it holds a reference, because a reference is an address, correct?

>I think perhaps you are actually talking about a variable here because a handle has a different definition.

If a handle holds a reference, does that mean it's not a variable? I don't understand.

#4 BobLewiston

BobLewiston
  • Topic Starter

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:05:10 AM

Posted 21 January 2009 - 11:28 PM

attention, groovicus:

In your opinion, does the following seem to indicate that some data types are NOT objects? Itís from
http://msdn.microsoft.com/en-us/library/yz2be5wk(VS.80).aspx :

Boxing and Unboxing (C# Programming Guide)

Boxing and unboxing enable value types to be treated as objects. Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap. Unboxing extracts the value type from the object. In this example, the integer variable i is boxed and assigned to object o.
int i = 123;
object o = (object)i;  // boxing
The object o can then be unboxed and assigned to integer variable i:
o = 123;
i = (int)o;  // unboxing


#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:10 AM

Posted 21 January 2009 - 11:51 PM

No. Everything is an object (in OOP).

I don't have time to respond to your second post at the moment.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users