0

Given an Array argument to a function passed in as object, how can I calculate its length in bytes? I can get the number of elements as int L = (arg as Array).Length; I've tried

Type t = (arg as Array).GetType().GetElementType();
int overallSize = sizeof(t);

This fails with:

The type or namespace name 't' could not be found (are you missing a using directive or an assembly reference?)

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
AAsk
  • 1,431
  • 4
  • 17
  • 25
  • http://stackoverflow.com/questions/605621/how-to-get-object-size-in-memory – Mojtaba Shateri Niasar Oct 31 '15 at 07:51
  • I deleted my answer because i just saw its marked as dupe. you can do `System.Runtime.InteropServices.Marshal.SizeOf(t)*arr.Length` – M.kazem Akhgary Oct 31 '15 at 07:52
  • Thanks M.kazem Akhgary, I believe Joey;s response is flawed - it deals with determining the size of a variable rather than what I am after, namely, determining the size of an array (passed as an object) when the type of its elements is known and the total number of elements is also known. – AAsk Oct 31 '15 at 08:11

0 Answers0