Stack<T>

Overview

Represents a last-in-first-out (LIFO) collection of objects.

Stack allows access only to top element of the collection. Stack accepts nil as a valid value for reference types and allows duplicate elements.

Note: On Nougat platform stack internally converts nil to the NSNull type.

Use case

You can use Stack<T> class when you need to create a LIFO queue:

var Numbers := new Stack<Integer>;
Numbers.Push(1);
Numbers.Push(2);
Numbers.Push(3);
Numbers.Push(4);

writeLn(Sugar.String.Format("Current: {0}", Numbers.Pop));
writeLn(Sugar.String.Format("Next on stack: {0}", Numbers.Peek));  

while Numbers.Count > 0 do
  writeLn(Sugar.String.Format("Current: {0}", Numbers.Pop));

Output:

Current: 4
Next on stack: 3
Current: 3
Current: 2
Current: 1

Loops

Please note: Instead of using for/each or for/in loops in the collections please use ForEach methods.

For example:

var Items := new List<String>(["One", "Two", "Three"]);
Items.ForEach(item -> DoSomething(item));  

Location

 

Clear  mapped

 

method Clear

 

void Clear()

 

func Clear()

 

void Clear()

 

Sub Clear()

Contains  mapped

 

method Contains(Item: T): Boolean

 

Boolean Contains(T Item)

 

func Contains(_ Item: T) -> Boolean

 

Boolean Contains(T Item)

 

Function Contains(Item As T) As Boolean

Parameters:

  • Item:

Count

 

property Count: Int32 read;

 

Int32 Count { get; }

 

var Count: Int32 { get{} }

 

Int32 Count { __get; }

 

ReadOnly Property Count() As Int32

Peek  mapped

 

method Peek: T

 

T Peek()

 

func Peek() -> T

 

T Peek()

 

Function Peek() As T

Pop  mapped

 

method Pop: T

 

T Pop()

 

func Pop() -> T

 

T Pop()

 

Function Pop() As T

Push  mapped

 

method Push(Item: T)

 

void Push(T Item)

 

func Push(_ Item: T)

 

void Push(T Item)

 

Sub Push(Item As T)

Parameters:

  • Item:

ToArray  mapped

 

method ToArray: array of T

 

T[] ToArray()

 

func ToArray() -> T...

 

T[] ToArray()

 

Function ToArray() As T()

 

Count

 

property Count: Int32 read;

 

Int32 Count { get; }

 

var Count: Int32 { get{} }

 

Int32 Count { __get; }

 

ReadOnly Property Count() As Int32

 

Clear  mapped

 

method Clear

 

void Clear()

 

func Clear()

 

void Clear()

 

Sub Clear()

Contains  mapped

 

method Contains(Item: T): Boolean

 

Boolean Contains(T Item)

 

func Contains(_ Item: T) -> Boolean

 

Boolean Contains(T Item)

 

Function Contains(Item As T) As Boolean

Parameters:

  • Item:

Peek  mapped

 

method Peek: T

 

T Peek()

 

func Peek() -> T

 

T Peek()

 

Function Peek() As T

Pop  mapped

 

method Pop: T

 

T Pop()

 

func Pop() -> T

 

T Pop()

 

Function Pop() As T

Push  mapped

 

method Push(Item: T)

 

void Push(T Item)

 

func Push(_ Item: T)

 

void Push(T Item)

 

Sub Push(Item As T)

Parameters:

  • Item:

ToArray  mapped

 

method ToArray: array of T

 

T[] ToArray()

 

func ToArray() -> T...

 

T[] ToArray()

 

Function ToArray() As T()