Home > Mobile >  how to get max and min value in stack csharp
how to get max and min value in stack csharp

Time:04-13

how I can get the max and min value in the stack using CSharp? I searched about that a lot I didn't find any way that can help me with it.

I'm using visual studio I made a form to enter numbers in a stack but I have no idea how to get the max number and min number in that stack

if anyone can help me with it:)

CodePudding user response:

Like this

        // load demo stack
        var s = new Stack<int>();
        s.Push(1);
        s.Push(2);
        s.Push(3);

now

        var minn = s.Min(); <<<==== get min
        var maxx = s.Max(); <<<=== get max

explanation

A Stack is an IEnumerable type so all LINQ extensions can be used on it

PS - note that you should not use the old Stack class, use Stack<int>; your comment suggested that you were using Stack

  • Related