dynamic length arrays

Z

Zytan

This article:
http://www.c-sharpcorner.com/Upload...Arrays11142005060354AM/WorkingWithArrays.aspx
claims that "In C#, arrays can be declared as fixed length or
dynamic".

I don't believe he is correct. With the exception of collections /
arraylists, can you have a dynamically sized array in C#? Note that i
am aware of Array.Resize<type>(ref myArray, newsize); for resizing
arrays in .NET 2.0. Is there a way to resize the array dynamically,
as Mahesh Chand claims (but doesn't show) in his article?

It mentions the System.Array class, which does hav a IsFixedSize
property, so maybe I can use that to make an array that is dynamic in
size.

(I think Mahesh Chand's article was written quickly, because he claims
the binary search will work without requirement of sorting. I've seen
this in other tutorials for VB, as well. It's like these things are
just written in 5 minutes and posted. And I have to register and log
in to write a comment to help him correct it.)

Zytan
 
Z

Zytan

Oh, and I already know about ArrayList. But, this article is talking
about arrays, not collections, which is what ArrayList is.

thanks,

Zytan
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Zytan said:
Ok, just to be clear, arrays are fixed in size? The IsFixedSize
property is only because it is required for the above?

Zytan

Yes.

It's actually impossible to resize an array in .NET. The methods that
are used for resizing an array are actually creating a new array and
copies the data to it.
 
Z

Zytan

It's actually impossible to resize an array in .NET. The methods that
are used for resizing an array are actually creating a new array and
copies the data to it.

thanks, Göran. I knew this, but thanks for being clear.

Zytan
 
B

Ben Voigt

Göran Andersson said:
Yes.

It's actually impossible to resize an array in .NET. The methods that are
used for resizing an array are actually creating a new array and copies
the data to it.

A .NET array is dynamic because the memory is dynamically allocated (from
the gc heap, or even from the stack), and the size is therefore determined
at runtime. That's all dynamic length means, not the ability to resize in
place.

OTOH, C# also supports fixed buffers, where the data is inline in the larger
type. Since the size is fixed at compile time, it fails the "dynamic" test.

http://blogs.msdn.com/ericgu/archive/2004/08/12/213676.aspx
 
Z

Zytan

A .NET array is dynamic because the memory is dynamically allocated (from
the gc heap, or even from the stack), and the size is therefore determined
at runtime. That's all dynamic length means, not the ability to resize in
place.

Thanks for clearing it up.
OTOH, C# also supports fixed buffers, where the data is inline in the larger
type. Since the size is fixed at compile time, it fails the "dynamic" test.

http://blogs.msdn.com/ericgu/archive/2004/08/12/213676.aspx

Wow, thanks, that's cool.

Zytan
 
Z

Zytan

OTOH, C# also supports fixed buffers, where the data is inline in the larger
type. Since the size is fixed at compile time, it fails the "dynamic" test.

http://blogs.msdn.com/ericgu/archive/2004/08/12/213676.aspx

C# is clear that the following is true:

"Pointers and fixed size buffers may only be used in an unsafe
context."
"Unsafe code may only appear if compiling with /unsafe."

So, it looks like its best to stay away from fixed size buffers.
But, thanks for letting us know about them.

Zytan
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Ben said:
A .NET array is dynamic because the memory is dynamically allocated (from
the gc heap, or even from the stack), and the size is therefore determined
at runtime. That's all dynamic length means, not the ability to resize in
place.

No, .NET arrays are not dynamic. They are dynamically allocated, but
that doesn't make them dynamic. A dynamic array is resizable.

http://en.wikipedia.org/wiki/Dynamic_array
 

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

Top