D
David Rogers
Is this sequence about as efficient as I can get for sometimes expanding an
array?
MyClass [] MyFunc()
{
MyClass [] result = new MyClass [0];
{
// Do stuff that might possibly allocate an array and put entries in
it
// This path is usually executed
// ...
result = new MyClass[numEntries];
}
{
// Do stuff that might possibly add more entries to the previously
allocated array
// This path may be executed, but not usually
// ...
newResult = new MyClass[result .Length + newNumEntries];
result .CopyTo(newResult , 0);
result = newResult ;
}
return result;
}
array?
MyClass [] MyFunc()
{
MyClass [] result = new MyClass [0];
{
// Do stuff that might possibly allocate an array and put entries in
it
// This path is usually executed
// ...
result = new MyClass[numEntries];
}
{
// Do stuff that might possibly add more entries to the previously
allocated array
// This path may be executed, but not usually
// ...
newResult = new MyClass[result .Length + newNumEntries];
result .CopyTo(newResult , 0);
result = newResult ;
}
return result;
}