> So if I have an array;
> int[] arr = new int[] {1,2,3,4};
> can I get the size of the array in bytes?
In basic, I can use this:
Dim aa() As Long = {1, 2, 3}
Dim ii As Integer = aa.Length * Marshal.SizeOf(aa(0))
You should be able to use a C equivalent.
source - 17 Mar 2006 19:39 GMT
Actually I was reading another C++ group and found someone asked how to find
the length of the array withouth using any inbuilt function
the solution that was suggested in C++ was
sizeof(arr)/sizeof(arr[0])
I can't seem to use sizeof for arrays in C# as it expects a value type
and if I try to use Marshal.SizeOf gives me argument exception.
So that made me think of asking this questoin.
unless I am doing something wrong in using sizeof
source
>> So if I have an array;
>> int[] arr = new int[] {1,2,3,4};
[quoted text clipped - 6 lines]
>
> You should be able to use a C equivalent.
What do you mean by value type? Array's are always reference types.
Willy.
| Is there a way to find the size of the entire array in .NET
| I tried using Marshal.SizeOf() but apprently it gives me an
[quoted text clipped - 5 lines]
|
| source
source - 17 Mar 2006 22:45 GMT
What I mean by that is
if I use Marshal.SizeOf(int) //value type
or
Marshal.SizeOf(arr[0])
it gives me argument exception
sorry if I was not clear on that
source
> What do you mean by value type? Array's are always reference types.
>
[quoted text clipped - 9 lines]
> |
> | source
Willy Denoyette [MVP] - 17 Mar 2006 23:52 GMT
Console.WriteLine(sizeof(int));
and:
int[] ia = new int[2] {0, 2};
Console.WriteLine(Marshal.SizeOf(ia[0]));
should both output 4.
Willy.
| What I mean by that is
| if I use Marshal.SizeOf(int) //value type
[quoted text clipped - 18 lines]
| > |
| > | source