array length question

M

Madhu

I would like to know how the following will be handled in the .net
framework:
Pl. don't pay attention to the syntax

int a[10]
int b

a[1]=2
a[2]=2
a[3]=2
a[5]=2
a[7]=2
a[8]=2

b=a.length % using the in-built function to find the array length


Rest of 'a' is not assigned any valu
I get the value of 'b' to be the length of first n values of 'a' which
have been initialized, i.e. the array is treated as complete once an
uninitialized array element is encountered.

Can someone give me an insight into why this is so?

Thank you.
 
G

Guest

Madhu,
First, this probably doesn't belong in ASP.NET since its a C# language
question and has virtually nothing to do with ASP.NET.

Second, let's do it right:

int[] a= new int[10];
int b=0;
a[1]=2;
a[2]=2;
a[3]=2;
a[5]=2;
a[7]=2;
a[8]=2;
b=a.Length;
Console.WriteLine(b);
Console.ReadLine();

In the example, b will ALWAYS be 10 because that is the Length of the Array
you've declared, whether you filled any, some or even none of the elements -
they all are initialized to their default type values.

Peter
 
M

Madhu

Peter said:
Madhu,
First, this probably doesn't belong in ASP.NET since its a C# language
question and has virtually nothing to do with ASP.NET.

Second, let's do it right:

int[] a= new int[10];
int b=0;
a[1]=2;
a[2]=2;
a[3]=2;
a[5]=2;
a[7]=2;
a[8]=2;
b=a.Length;
Console.WriteLine(b);
Console.ReadLine();

In the example, b will ALWAYS be 10 because that is the Length of the Array
you've declared, whether you filled any, some or even none of the elements -
they all are initialized to their default type values.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Madhu said:
I would like to know how the following will be handled in the .net
framework:
Pl. don't pay attention to the syntax

int a[10]
int b

a[1]=2
a[2]=2
a[3]=2
a[5]=2
a[7]=2
a[8]=2

b=a.length % using the in-built function to find the array length


Rest of 'a' is not assigned any valu
I get the value of 'b' to be the length of first n values of 'a' which
have been initialized, i.e. the array is treated as complete once an
uninitialized array element is encountered.

Can someone give me an insight into why this is so?

Thank you.

Thanks Peter. Sorry about the post to the wrong forum. I don't do any
coding work, I'm more a Business side guy. I was trying to help one of
my developers.
Would the length returned be the same even if we are dealing with an
array of objects?

If my question doesn't make sense/is not rigorously stated, never mind
making sense out of it. I'm just putting it the way I understood the
problem.

Thanks
 
L

Laurent Bugnion

Hi,
Thanks Peter. Sorry about the post to the wrong forum. I don't do any
coding work, I'm more a Business side guy. I was trying to help one of
my developers.
Would the length returned be the same even if we are dealing with an
array of objects?

If my question doesn't make sense/is not rigorously stated, never mind
making sense out of it. I'm just putting it the way I understood the
problem.

Thanks

Yes it will. When you create a new array with a given length, the memory
is allocated for this array according to the type of the content. For
objects, the memory is allocated for 10 "handles" (references) to
objects. When you use "Length", it will return 10, even if only 8 cells
are filled up.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top