开发者

How to unbox a System.Int32[*] array

Firebird rdr.Read() returns, for fb array column开发者_运维百科s, an object witch is in my case System.Int32[*] (non-zero based array of ints), how do I unbox it into something sane? Connector does not have any rdr.GetZeroBasedArray() or rdr.GetUsableArray() method.

Thx in advance...


C# has no support for [*]-style (non-zero-based one-dimensional) arrays. Trying to cast to int[] will throw an exception.

The only option you have is to cast to System.Array and then use these methods to modify it:

  • Array.GetValue instead of array[i]

  • Array.SetValue instead of array[i] = x;

Alternatively, of course, you can always use Array.GetLength to find the length of the array, instantiate a standard int[] of the same length, and then use Array.Copy to copy the data over. Then you can use array[i] normally, but be aware that you are now operating on a separate copy.

P.S. As already pointed out, unbox is the wrong word. Boxing/unboxing is used only with value types, but arrays are always reference types (even arrays of value types are reference types). What you are looking for is called a cast. The Array variable will contain a reference to the same object as the object variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜