all groups > dotnet clr > march 2006 >
You're in the

dotnet clr

group:

Size of Array in bytes


Size of Array in bytes source
3/17/2006 1:15:18 AM
dotnet clr: 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
ArgumentException if I use a value type.
So if I have an array;

int[] arr = new int[] {1,2,3,4};
can I get the size of the array in bytes?

source

RE: Size of Array in bytes AMercer
3/17/2006 8:51:17 AM
[quoted text, click to view]

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.
Re: Size of Array in bytes source
3/17/2006 10:39:34 AM
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

[quoted text, click to view]

Re: Size of Array in bytes source
3/17/2006 1:45:15 PM
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

[quoted text, click to view]

Re: Size of Array in bytes Willy Denoyette [MVP]
3/17/2006 9:28:06 PM
What do you mean by value type? Array's are always reference types.

Willy.

[quoted text, click to view]
| 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
| ArgumentException if I use a value type.
| So if I have an array;
|
| int[] arr = new int[] {1,2,3,4};
| can I get the size of the array in bytes?
|
| source
|
|

Re: Size of Array in bytes Willy Denoyette [MVP]
3/17/2006 11:52:02 PM
Console.WriteLine(sizeof(int));
and:
int[] ia = new int[2] {0, 2};
Console.WriteLine(Marshal.SizeOf(ia[0]));

should both output 4.

Willy.



[quoted text, click to view]
| 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
|
[quoted text, click to view]
| > What do you mean by value type? Array's are always reference types.
| >
| > Willy.
| >
[quoted text, click to view]
| > | 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
| > | ArgumentException if I use a value type.
| > | So if I have an array;
| > |
| > | int[] arr = new int[] {1,2,3,4};
| > | can I get the size of the array in bytes?
| > |
| > | source
| > |
| > |
| >
| >
|
|

AddThis Social Bookmark Button