Skip to content

Instantly share code, notes, and snippets.

@rajibchy
Last active February 17, 2026 23:24
Show Gist options
  • Select an option

  • Save rajibchy/63d101a0530484b094f2ac179167aca2 to your computer and use it in GitHub Desktop.

Select an option

Save rajibchy/63d101a0530484b094f2ac179167aca2 to your computer and use it in GitHub Desktop.
Pop, Shift and Push off array in C# equivalent of `Javascript` `Array`
// Copyright (c) 2022 Safe Online World Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// 4:01 PM 1/30/2023
// by rajib chy
using System;
using System.Collections.Generic;
namespace Sow.Framework {
class ArrayStack<T> {
List<T> _list;
public int Count => _list.Count;
public T this[ int index ] {
get {
int count = _list.Count;
if ( ( uint )index >= ( uint )count ) {
throw new ArgumentOutOfRangeException( nameof( index ), "Index was out of range. Must be non-negative and less than the size of the collection" );
}
return _list[ index ];
}
set {
int count = _list.Count;
if ( ( uint )index >= ( uint )count ) {
throw new ArgumentOutOfRangeException( nameof( index ), "Index was out of range. Must be non-negative and less than the size of the collection" );
}
_list[ index ] = value;
}
}
public ArrayStack( ) {
_list = new List<T>( );
}
public void Push( T item ) {
_list.Add( item );
}
public T Shift( ) {
if ( _list.Count == 0 ) {
// no element found
return default( T );
}
T item = _list[ 0 ];
_list.RemoveAt( 0 );
return item;
}
public T Pop( ) {
int lastIndex = _list.Count - 1;
if ( lastIndex < 0 ) {
// no rest of element
return default( T );
}
T item = _list[ lastIndex ];
_list.RemoveAt( lastIndex );
return item;
}
}
}
// see more https://stackoverflow.com/questions/455237/pop-off-array-in-c-sharp/75277612#75277612
ArrayStack<int> array = new ArrayStack<int>( );
array.Push( 10 ); // add new item
array.Push( 11 ); // add new item
array.Push( 12 ); // add new item
int item = array[ 0 ]; // 10
item = array.Shift( ); // 10; remove and get first item
item = array.Pop( ); // 12; remove and get last item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment