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 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()

Contains  mapped

 

method Contains(Item: T): Boolean

 

Boolean Contains(T Item)

 

func Contains(_ Item: T) -> Boolean

 

Boolean Contains(T Item)

Parameters:

  • Item:

Count

 

property Count: Int32 read;

 

Int32 Count { get; }

 

var Count: Int32 { get{} }

 

Int32 Count { __get; }

Peek  mapped

 

method Peek: T

 

T Peek()

 

func Peek() -> T

 

T Peek()

Pop  mapped

 

method Pop: T

 

T Pop()

 

func Pop() -> T

 

T Pop()

Push  mapped

 

method Push(Item: T)

 

void Push(T Item)

 

func Push(_ Item: T)

 

void Push(T Item)

Parameters:

  • Item:

ToArray  mapped

 

method ToArray: array of T

 

T[] ToArray()

 

func ToArray() -> T...

 

T[] ToArray()

 

Count

 

property Count: Int32 read;

 

Int32 Count { get; }

 

var Count: Int32 { get{} }

 

Int32 Count { __get; }

 

Clear  mapped

 

method Clear

 

void Clear()

 

func Clear()

 

void Clear()

Contains  mapped

 

method Contains(Item: T): Boolean

 

Boolean Contains(T Item)

 

func Contains(_ Item: T) -> Boolean

 

Boolean Contains(T Item)

Parameters:

  • Item:

Peek  mapped

 

method Peek: T

 

T Peek()

 

func Peek() -> T

 

T Peek()

Pop  mapped

 

method Pop: T

 

T Pop()

 

func Pop() -> T

 

T Pop()

Push  mapped

 

method Push(Item: T)

 

void Push(T Item)

 

func Push(_ Item: T)

 

void Push(T Item)

Parameters:

  • Item:

ToArray  mapped

 

method ToArray: array of T

 

T[] ToArray()

 

func ToArray() -> T...

 

T[] ToArray()