|
-
Aug 9th, 2007, 09:37 PM
#1
Thread Starter
Fanatic Member
Doubt...Need explaination
I just saw a piece of code with following two lines. Can anyone explain what does that mean? I understand the first line but what does 2nd line do?
Code:
temp = new byte[2];
byte[] temp2;
-
Aug 9th, 2007, 10:12 PM
#2
Re: Doubt...Need explaination
The first line creates a byte array with 2 elements and assigns it to the temp variable. The second line declares the temp2 variable as type byte array. That means that, just like temp, temp2 is a byte array variable to which you can assign a byte array object. Before the first line temp was a byte array variable that didn't refer to any object. After the first line temp is a byte array variable that does refer to an array object. After the second line temp2 is a byte array variable that doesn't refer to any object.
-
Aug 9th, 2007, 10:22 PM
#3
Thread Starter
Fanatic Member
Re: Doubt...Need explaination
That was confusing
After reading 2-3 times....I got it 
Thanks.
-
Aug 10th, 2007, 01:45 AM
#4
Re: Doubt...Need explaination
It's important to understand the distinction between variables and objects. When you declare things with names in your code, like temp and temp2, you are declaring variables. Variables have names. Variables are stored in memory on the stack. Variables can either contain a value, like numbers or dates, or they can contain a reference to an object.
An object is something else. Objects don't have names and they are stored in memory on the heap. When you create an object you may or may not assign it to a variable. Assigning an object to a variable means storing the object's memory address in the variable, thus the variable refers to the object. Multiple variables can refer to the same object because they can contain the same memory address. Variables can also refer to no object, in which case they are called a null reference.
Arrays are objects in .NET, thus they are stored on the heap. In order to create an array object you must specify, either explicitly or implicitly, the number of elements it contains. The system cannot create the array if it doesn't know big to make it.
Like I said, the first line creates an object and assigns it to an existing variable, while the second line simply declares a variable with no object involved.
-
Aug 10th, 2007, 12:04 PM
#5
Thread Starter
Fanatic Member
Re: Doubt...Need explaination
Good explanation. I understand it now. Thanks a lot.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|