2

How is the size of an individual Array element retrieved in C#?

Int16[] a1 = new Int16[1024];
Int16[] a2 = new byte[1024];
Array array = ...  // (either a1 or a2)

int elementSizeInBytes = array.____?

sizeof will not work, since sizeof is a compile-time operator -- sizeof(array) or sizeof(array[0]) do not even compile, of course.

It's straightforward to get the number of elements:

int numElements = array.Length;

There doesn't appear to be a method to return the element size. It would then seem to be calculable from the total space allocated for the array / array.Length. But it isn't obvious how to get the total size in bytes used by an Array instance.


Example Usage:

The question is a general one, but the current way I'm trying to use it is to copy the contents of an array buffer to a WriteableBitmap:

private void copyPixels2Bitmap(Array pixels, WriteableBitmap bitmap)
{
    int elementSize = pixels.GetValue(0).GetType().___?;
    bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), 
        pixels, bitmap.PixelWidth * elementSize, 0);
}
5
  • 4
    sizeof Commented Jun 2, 2013 at 19:09
  • it's not that straight forward because it's not that simple.. what do you intent to do with that size information? Commented Jun 2, 2013 at 19:11
  • @ErenErsönmez Copy the data. In this case, pass it to bitmap.WritePixels() which requires the stride (width * elementSize). Commented Jun 2, 2013 at 19:14
  • This answer stackoverflow.com/a/8173293/1099260 on the topic was up voted 9 times as has some cool points Commented Jun 2, 2013 at 19:42
  • You seem to be assuming there is more data allocated than just the data of the elements themselves, i.e. that an array of 1024 Int16 elements would take up more than 1024 * 16 bits. There's a pointer to that data as well, but that's not part of the array itself. Commented Jun 2, 2013 at 22:07

3 Answers 3

3

This is the best way I've found so far:

int elementSize = System.Runtime.InteropServices.Marshal.SizeOf(pixels.GetValue(0));
Sign up to request clarification or add additional context in comments.

Comments

3

Rewrite this method to use generics.

private void copyPixels2Bitmap(Array pixels, WriteableBitmap bitmap)
{
    int elementSize = pixels.GetValue(0).GetType().___?;
    bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), 
        pixels, bitmap.PixelWidth * elementSize, 0);
}

Becomes:

private void CopyPixelsToBitmap<T>(T[] pixels, WriteableBitmap bitmap)
  where T: struct
{
    var elementSize = Marshal.SizeOf(typeof(T));
    bitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), 
        pixels, bitmap.PixelWidth * elementSize, 0);

}

Works for any value type.

2 Comments

+1 for solving the example usage. Very nice. This would be the answer, except that it answers a slightly different question -- how to get the size of an element of a generic array. Still, a great answer.
Given an array e.g. int[] foo You can get the size of the element type (in this case int) via: if (foo.GetType().IsArray) { var size = Marshal.SizeOf(foo.GetType().GetElementType()); }
1

Int16[] array = new Int16[1024];

This means that you are allocating 1024*16bits spaces in memory, wich actually means that each element of the array can be up to 2^16 - 1. If you are looking for memory space occupied you can use sizeof.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.