{"id":501,"date":"2023-08-22T09:15:00","date_gmt":"2023-08-22T15:15:00","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=501"},"modified":"2023-08-22T09:15:00","modified_gmt":"2023-08-22T15:15:00","slug":"stack-using-generics-in-c","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/stack-using-generics-in-c\/","title":{"rendered":"Stack using generics in C#"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">using System;\nusing System.Collections.Generic;\n\nclass StackExample&lt;T>\n{\n    private List&lt;T> items = new List&lt;T>();\n\n    public void Push(T obj)\n    {\n        items.Add(obj);\n    }\n\n    public T Pop()\n    {\n        if (items.Count == 0)\n        {\n            throw new InvalidOperationException(\"The stack is empty...\");\n        }\n\n        int lastIndex = items.Count - 1;\n        T poppedItem = items[lastIndex];\n        items.RemoveAt(lastIndex);\n        return poppedItem;\n    }\n\n    public void Clear()\n    {\n        items.Clear();\n    }\n}\n\nclass Program\n{\n    static void Main(string[] args)\n    {\n        StackExample&lt;int> stack1 = new StackExample&lt;int>();\n\n        stack1.Push(11);\n        stack1.Push(22);\n        stack1.Push(33);\n\n        Console.WriteLine(\"Popped \" + stack1.Pop()); \/\/ 33\n\n        stack1.Push(44);\n\n        Console.WriteLine(\"Popped  \" + stack1.Pop()); \/\/ 44\n\n        stack.Clear();\n    }\n}\n<\/code><\/pre><\/div><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":463,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[36,41,51,32,52,7],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/501"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=501"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/501\/revisions"}],"predecessor-version":[{"id":502,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/501\/revisions\/502"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/463"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}