I'm being passed an object that returns "System.Byte[*]" when converted to string. This apparently isn't a standard one dimensional array of Byte objects ("System.Byte[]"), so what is it?
A:
There is no ...[*]
type in c# as far as I remember. Maybe you can find the API documentation for that library, that will show you what the type is really. Even if you don't have that, Visual Studio should show you the type when you try autocompletion on the method returning the object.
viraptor
2010-03-15 17:00:17
The object is passed in as object. I tried to catch it with if (o.GetType() == typeof(Byte[]))but this didn't work. (Though it did work with I explicitly passed in my own Byte[].
Jimbo
2010-03-15 17:02:17
+7
A:
That's probably a single-dimensional array with a non-zero base.
Here's an example of how to create one:
using System;
class Test
{
static void Main()
{
Array nonZeroBase = Array.CreateInstance
(typeof(byte), new int[]{1}, new int[]{2});
Console.WriteLine(nonZeroBase); // Prints byte[*]
}
}
In CLR terminology this is called an array (rectangular arrays are also arrays) where single-dimensional, zero-based arrays are called vectors. (A multi-dimensional array would be printed as byte[,]
though.)
You may be interested in this blog post which Marc Gravell posted just morning...
Jon Skeet
2010-03-15 17:01:06
I didn't realize this construct was still around; it must be something for VB.NET. I wonder how many libraries are out there are that would break if given one of these as a parameter. I certainly always thought a for loop from 0 to Length-1 was safe.
Dan Bryant
2010-03-15 17:35:34
@Dan: You wouldn't be able to cast this to byte[] to start with; it would only be a problem if you received it as `Array` instead.
Jon Skeet
2010-03-15 17:38:24
When I add byte[] b = nonZeroBase as byte[]; b is null. When I do the exact same thing in the immediate window, it works. Why is this?
Jimbo
2010-03-15 17:41:27
To clarify, I execute b = nonZeroBase as byte[]; in the immediate window and it is no longer null. I can access b[2]'s value this way. Strange that the compiled code doesn't work the same way as the immediate window.
Jimbo
2010-03-15 17:58:12
@Jimbo: I suspect the immediate window is doing some munging... sounds a bit odd to me. I can't say I use it much myself, so I don't know details about what it does.
Jon Skeet
2010-03-15 18:26:33