Array Manipulation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to code a method to which will be passed an array. I need help on:

1) How do I discover if the passed array is a value type array ?
2) How do I discover the type of each element of the passed array ?
3) How do I discover how many bytes the passed array has ?
4) How do I copy the passed array to a byte[] ?

Thanks.

_____
Marco
 
Marco,
1) How do I discover if the passed array is a value type array ?
arr.GetElementType().IsValueType


2) How do I discover the type of each element of the passed array ?

Well if the array element type is a value type or a sealed reference
type then every element is simply the element type. Otherwise loop
through the array and call GetType on each element. Assuming a one
dimensional array:

foreach (object o in nonSealedRefTypeArray)
if (o != null) Console.WriteLine(o.GetType());

3) How do I discover how many bytes the passed array has ?

That depends on the runtime implementation.

4) How do I copy the passed array to a byte[] ?

For 1D arrays of primitive numerical types you can use
Buffer.BlockCopy. For other arrays it's more complicated or not
possible at all.

What kind of arrays are you working with?


Mattias
 
Back
Top