HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

C# Array Mutator Methods to mimic JavaScript Array Mutator Methods

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
mimicarrayjavascriptmethodsmutator

Problem

Between .NET's built-in collection libraries and LINQ extension methods, I never have to use arrays. An unfortunate consequence of this is that I am probably less competent in working with this very important Type than I should be. To help myself get a better grip on the Array class, I wrote some C# helper methods that mimic the "Mutator" methods in the JavaScript array type. These methods are documented thoroughly at https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array . I omitted the methods that already have analogous implementations in .NET, (including reverse and sort). I also tried to avoid using LINQ, on which I have grown pretty dependent. (LINQ also manipulates arrays under the hood, so a properly written direct approach should perform better).

I appreciate : (1) any feedback and/or highlighting of anywhere I may have messed up, (2) highlighting of any errors that may not necessarily but could potentially cause problems (such as thread-safety issues); (3) any optimization techniques that I should have used; and, (4) anything I did that was generally stupid.

I use an IsNullOrEmpty extension method that is equivalent to ReferenceEquals(array,null) || array.Length == 0

```
public static class ArrayMutator
{
public static T Pop(ref T[] array)
{
Contract.Requires(!array.IsNullOrEmpty());
T result = array[array.Length - 1];
if (array.Length == 1)
{
array = new T[0];
}
else
{
var popped = new T[array.Length - 1];
System.Array.Copy(array, 0, popped, 0, array.Length - 1);
array = popped;
}
return result;
}

public static int Push(ref T[] array, T element)
{
if (array.IsNullOrEmpty())
{
array = new T[1] {element};
return 1;
}
var pushed = new T[array.Length + 1];
array.CopyTo(pushed, 0);
pushed[array.Length] =

Solution

Your code seems fine to me, just a few things I would do differently:

-
Your usage of ConstrainedCopy(), instead of normal Copy(), doesn't make sense to me. This method is useful if you want to guarantee that the target array is in a consistent state even if an error happens during processing. But you're always modifying a temporary array, so you don't care about its state after an error.

Using ConstrainedCopy() will be less performant (although probably not significantly) and it's more confusing (it makes me think why you didn't use normal Copy()).

-
When creating an array using array initializer, you don't need to specify the size: new T[] {element} works fine.

Also, if you used your methods a lot, your application would have bad performance because of all that copying. If you ever wanted to actually use something like this, you should do what List does: when adding, resize to twice the size of the original array, so that you don't have to copy the whole array the next time you add to it. But you're probably aware of this.

Context

StackExchange Code Review Q#15789, answer score: 3

Revisions (0)

No revisions yet.