C# 64-Bit Array.Clear
In version 3.5 of the NET framework, and possibly in earlier releases, there are variants of Array.Copy
that accept 64-bit values for index and length. Unfortunately, Array.Clear
has no such variant. This seems to leave only bad options for clearing an array using 64-bit indexes and lengths.
One option is to cast the arguments to Int32
values in each call, like this:
UInt[] a = something;
Int64 index = something;
Int64 length = something;
Array.Cear(a, ((Int32) index), ((Int32) length);
Another option is to make an ad-hoc method and call that instead of Array.Clear
, e.g., make a method like the following, and call it instead of Array.Clear
:
static void zero(
UInt64[] a
, Int64 index
, Int64 length
)
{
Array.Clear(
a
, checked((Int32) index)
, checked((Int32) length)
);
}
Another possibility is as follows.
static void zero(
UInt64[] a
, Int64 index
, Int64 length
)
{
for (Int64 c = 0; c < length; c++, index++)
{ a[index] = 0; }
}
None of these options are good. The first two will not work when the index or length will not fit in 32 bits. The latter will work, but does not have whatever built-in speed benefits Array.Clear
has, and, creates two ways of zeroing arrays – one with zero
, another with Array.Clear
. There are other problems with these solutions as well.
It would be better if the framework added a variant of Array.Clear
that accepted 64-bit arguments for index and length. Even if, for some temporary period, this variant threw an exception when the value was not a legal 32 bit value, it would still be better in at least some applications to call this method than to resort to one of the above workarounds.
Posted on February 20th, 2008 by Andrew
Under C# / Common Language Infrastructure