Connecting Tech Pros Worldwide Forums | Help | Site Map

dynamic length arrays

Zytan
Guest
 
Posts: n/a
#1: Feb 25 '07
This article:
http://www.c-sharpcorner.com/UploadF...ithArrays.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


Zytan
Guest
 
Posts: n/a
#2: Feb 25 '07

re: dynamic length arrays


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

thanks,

Zytan


Tim Clark
Guest
 
Posts: n/a
#3: Feb 25 '07

re: dynamic length arrays


Correct:
http://msdn2.microsoft.com/en-us/lib...fixedsize.aspx

"Array implements the IsFixedSize property because it is required by the
System.Collections.IList interface."
Zytan
Guest
 
Posts: n/a
#4: Feb 25 '07

re: dynamic length arrays


Correct:http://msdn2.microsoft.com/en-us/lib...fixedsize.aspx
Quote:
>
"Array implements the IsFixedSize property because it is required by the
System.Collections.IList interface."
Ok, just to be clear, arrays are fixed in size? The IsFixedSize
property is only because it is required for the above?

Zytan

=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#5: Feb 25 '07

re: dynamic length arrays


Zytan wrote:
Quote:
Quote:
>Correct:http://msdn2.microsoft.com/en-us/lib...fixedsize.aspx
>>
>"Array implements the IsFixedSize property because it is required by the
>System.Collections.IList interface."
>
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.

--
Göran Andersson
_____
http://www.guffa.com
Zytan
Guest
 
Posts: n/a
#6: Feb 25 '07

re: dynamic length arrays


It's actually impossible to resize an array in .NET. The methods that
Quote:
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

Ben Voigt
Guest
 
Posts: n/a
#7: Feb 27 '07

re: dynamic length arrays



"Göran Andersson" <guffa@guffa.comwrote in message
news:esE0GfIWHHA.600@TK2MSFTNGP05.phx.gbl...
Quote:
Zytan wrote:
Quote:
Quote:
>>Correct:http://msdn2.microsoft.com/en-us/lib...fixedsize.aspx
>>>
>>"Array implements the IsFixedSize property because it is required by the
>>System.Collections.IList interface."
>>
>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.
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...12/213676.aspx
Quote:
>
--
Göran Andersson
_____
http://www.guffa.com

Zytan
Guest
 
Posts: n/a
#8: Feb 27 '07

re: dynamic length arrays


A .NET array is dynamic because the memory is dynamically allocated (from
Quote:
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.
Quote:
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...12/213676.aspx
Wow, thanks, that's cool.

Zytan

Zytan
Guest
 
Posts: n/a
#9: Feb 27 '07

re: dynamic length arrays


OTOH, C# also supports fixed buffers, where the data is inline in the larger
Quote:
type. Since the size is fixed at compile time, it fails the "dynamic" test.
>
http://blogs.msdn.com/ericgu/archive...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?=
Guest
 
Posts: n/a
#10: Feb 27 '07

re: dynamic length arrays


Ben Voigt wrote:
Quote:
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

--
Göran Andersson
_____
http://www.guffa.com
Zytan
Guest
 
Posts: n/a
#11: Feb 27 '07

re: dynamic length arrays


No, .NET arrays are not dynamic. They are dynamically allocated, but
Quote:
that doesn't make them dynamic. A dynamic array is resizable.
>
http://en.wikipedia.org/wiki/Dynamic_array
Göran, you are completely right. This distinction is important. I
wish I could change my earlier posts.

Thank you.

Zytan

Closed Thread