Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / C#

System.Collections.Generic.Stack: riferimento pratico

System.Collections.Generic.Stack: riferimento pratico: Stack<T> implementa l’ordine last-in, first-out tramite Push, Peek e Pop.

Versione: .NET API; verified on .NET 10.0.14

Sintassi

System.Collections.Generic.Stack /* select a documented member or constructor */

Stack implementa l’ordine last-in, first-out tramite Push, Peek e Pop.

Usalo per ricerca in profondità, cronologie di annullamento e modellazione esplicita di uno stack di lavoro.

Non è sicuro per scritture concorrenti e l’enumerazione osserva gli elementi senza rimuoverli.

Pop e Peek su uno stack vuoto generano InvalidOperationException, mentre TryPop e TryPeek restituiscono false.

Il programma completo usa realmente System.Collections.Generic.Stack; lo stdout esatto atteso è 2.

Esempi

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var stack = new Stack<int>();
stack.Push(1); stack.Push(2);
Console.WriteLine(stack.Pop());

Risultato

2

Il programma completo usa realmente `System.Collections.Generic.Stack`; lo stdout esatto atteso è `2`.

Fonti