Koleksi objek yang diwakili oleh keluar terakhir dan keluar pertama dipanggil Tindanan dan ia adalah koleksi yang meningkat dengan penambahan elemen pada tindanan mengikut keperluan program, oleh itu ia adalah koleksi dinamik dan elemen dari kedua-dua jenis yang sama dan jenis yang berbeza boleh disimpan dalam timbunan dan proses menambah elemen pada timbunan dipanggil menolak elemen ke timbunan dan proses mengeluarkan elemen dari timbunan dipanggil meletuskan elemen dari timbunan dan timbunan ini berada di bawah Sistem. Ruang nama koleksi.
Sintaks:
Sintaks C# Stack adalah seperti berikut:
Stack stack_name = new Stack();
Di mana stack_name ialah nama stack.l
Terdapat beberapa pembina dalam Stack dalam C#. Mereka ialah:
Terdapat beberapa kaedah dalam Stack dalam C#. Mereka ialah:
Berikut ialah contoh tindanan c# :
Pertimbangkan contoh atur cara di bawah untuk menunjukkan kaedah Push(), kaedah Pop(), kaedah Peek(), kaedah Mengandungi() dan kaedah Clear():
Kod:
using System; using System.Collections; //a class called program is defined class program { //main method is called public static void Main() { //a new stack is created Stack mystk = new Stack(); //Adding the elements to the newly created stack mystk.Push("India"); mystk.Push("USA"); mystk.Push("Canada"); mystk.Push("Germany"); //displaying the elements of the stack using foreach loop Console.Write("The elements in the Stack are : "); foreach(varele in mystk) { Console.WriteLine(ele); } //using contains() method to check if an element is present in the stack or not Console.WriteLine(mystk.Contains("Germany")); // The count of the elements in the stack is displayed Console.Write("The count of elements in the Stack are : "); Console.WriteLine(mystk.Count); // displaying the top most element of the stack using Peek() method Console.WriteLine("The topmost element in the stack is : " + mystk.Peek()); //Using pop() method to remove the top element in the stack Console.WriteLine("the element of the stack that is going to be removed" + " is: {0}",mystk.Pop()); Console.Write("The elements in the Stack after using pop() method are : "); foreach(var el in mystk) { Console.WriteLine(el); } Console.Write("The count of elements in the Stack after using pop() method is : "); Console.WriteLine(mystk.Count); //using Clear() method to remove all the elements in the stack mystk.Clear(); Console.Write("The count of elements in the Stack after using Clear() method is : "); Console.WriteLine(mystk.Count); } }
Output:
Dalam program di atas, kelas yang dipanggil program ditakrifkan. Kemudian kaedah utama dipanggil. Kemudian timbunan baru dibuat. Kemudian elemen ditambah ke dalam timbunan yang baru dibuat menggunakan kaedah Push(). Kemudian elemen timbunan yang baru dibuat dipaparkan menggunakan gelung foreach. Kemudian kaedah contains() digunakan untuk menyemak sama ada elemen hadir dalam timbunan atau tidak. Kemudian kiraan elemen dalam timbunan dipaparkan dengan menggunakan kaedah count(). Kemudian elemen paling atas timbunan dipaparkan menggunakan kaedah Peek(). Kemudian elemen paling atas timbunan dikeluarkan menggunakan kaedah Pop(). Kemudian sekali lagi kiraan elemen dan elemen timbunan dipaparkan selepas menggunakan kaedah Pop(). Kemudian kaedah Clear() digunakan untuk mengalih keluar semua elemen timbunan. Kemudian sekali lagi kiraan elemen dan elemen timbunan dipaparkan selepas menggunakan kaedah Clear(). Output program ditunjukkan dalam syot kilat di atas.
Consider the example program below to demonstrate Clone() method, Equals() method, Synchronized() method, CopyTo() method, ToArray() method, GetType() method and GetEnumerator() method:
Code:
using System; using System.Collections; //a class called program is defined class program { // Main Method is called public static void Main(string[] args) { // creating a new stack Stack mystk = new Stack(); mystk.Push("India"); mystk.Push("USA"); mystk.Push("Canada"); mystk.Push("Germany"); Console.Write("The elements in the Stack are : "); foreach(varele in mystk) { Console.WriteLine(ele); } // a clone of the newly created stack is created Stack mystk1 = (Stack)mystk.Clone(); // the top most element of the clone of the newly created stack is removed using pop() method mystk1.Pop(); Console.Write("The elements in the clone of the Stack after using pop() method are : "); //the elements of the clone of the newly created stack is displayed foreach(Object ob in mystk1) Console.WriteLine(ob); //checking if the elements of the clone of the newly created stack and the newly created stack are equal or not Console.Write("The elements in the clone of the Stack and the stack are equal or not : "); Console.WriteLine(mystk.Equals(mystk1)); //Checking if the clone of the newly created stack and the newly created stack is synchronised or not Console.WriteLine("The Clone of the newly created stack is {0}.", mystk1.IsSynchronized ? "Synchronized" : "Not Synchronized"); Console.WriteLine("The newly created stack is {0}.", mystk.IsSynchronized ? "Synchronized" : "Not Synchronized"); //a new array of strings is created and the newly created stack is assigned to this array string[] arra = new string[mystk.Count]; // The elements of the newly created stack is copied to the array mystk.CopyTo(arra, 0); // the elements of the array are displayed Console.Write("The elements of the array copied from the newly created stack are : "); foreach(string st in arra) { Console.WriteLine(st); } //converting the elements of the newly created stack to array using toarray() method Object[] ar1 = mystk.ToArray(); Console.Write("The elements of the array copied from the newly created stack by using ToArray() method are :"); //the elements of the array are displayed foreach(Object st1 in ar1) { Console.WriteLine(st1); } Console.WriteLine("The type of newly created stack before using "+ "ToStringMethod is: "+mystk.GetType()); Console.WriteLine("The type of newly created stack after using "+ "ToString Method is: "+mystk.ToString().GetType()); Console.Write("The elements of the newly created stack after using GetEnumerator() method are : "); //Getenumerator() method is used to obtain the enumerator of thestack IEnumeratorenume = mystk.GetEnumerator(); while (enume.MoveNext()) { Console.WriteLine(enume.Current); } } }
Output:
In the above program, a class called program is defined. Then the main method is called. Then a new stack is created. Then the clone of the newly created stack is created by using the clone() method. Then the topmost element of the clone of the newly created stack is removed using the pop() method. Then the elements of the clone of the newly created method are displayed. Then Equals() method is used to check if the newly created stack and the clone of the newly created stack are equal or not. Then the synchronized() method is used to check if the newly created stack and the clone of the newly created stack are synchronized or not. Then Copyto() method is used to copy the newly created stack to an array and the elements of the array are displayed. Then ToArray() method is used to copy the newly created stack to another array and then the elements of the array are displayed. Then GetType() method is used to obtain the type of the newly created stack. Then ToString() method is used to convert the type stack to string. Then GetEnumerator() method is used to obtain the IEnumerator of the stack. The output of the program is shown in the snapshot above.
Atas ialah kandungan terperinci C# Stack. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!