Does Stack <> create a reverse stack constructor when initializing from another?

Here is the code:

var s = new Stack<int>();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);

var ns = new Stack<int>(s);
var nss = new Stack<int>(new Stack<int>(s));

and then look at the result

        tbLog.Text += "s stack:";
        while(s.Count > 0)
        {
            tbLog.Text += s.Pop() + ",";
        }
        tbLog.Text += Environment.NewLine;
        tbLog.Text += "ns stack:";
        while (ns.Count > 0)
        {
            tbLog.Text += ns.Pop() + ",";
        }

        tbLog.Text += Environment.NewLine;
        tbLog.Text += "nss stack:";
        while (nss.Count > 0)
        {
            tbLog.Text += nss.Pop() + ",";
        }

outputs the following result:

s stack:4,3,2,1,

ns stack:1,2,3,4,

nss stack:4,3,2,1,

So, the stack nsreturns sand the nssstack matches the stack s.

+5
source share
3 answers

The stack constructor that accepts IEnumerable<T>pushes the elements as if they Addwere called several times.

""... , , , " " .., .

+12

, ns a nss, - Stack<T>(IEnumerable<T>). , . . , ( ) .

, , .

+3

:

Stack < > ?

, , Stack - , IEnumerable. Stack Stack a Stack IEnumerable.

, Stack Stack, Stack , popping. Stack IEnumerable. , , .

+2

All Articles