Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2006

Tip: Looking for answers? Try searching our database.

Size of Array in bytes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
source - 17 Mar 2006 10:15 GMT
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
AMercer - 17 Mar 2006 17:51 GMT
> 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.
Willy Denoyette [MVP] - 17 Mar 2006 21:28 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.