Step : 2
Class: StackClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
/// <summary>
/// Summary description for StackClass
/// </summary>
public class StackClass
{
Stack myStack = new Stack();
IEnumerable myPushCollection;
string strPush, strPop, strPeek;
public StackClass()
{
//
// TODO: Add constructor logic here
//
}
public void Add_Push(string str)
{
myStack.Push(str);
}
public void POP()
{
myStack.Pop();
}
public void Peek()
{
myStack.Peek();
}
public string Print_Push()
{
myPushCollection = myStack;
foreach (Object obj in myPushCollection)
{
strPush += " " + obj.ToString();
}
return strPush;
}
public string Print_Peek()
{
myPushCollection = myStack;
foreach (Object obj in myPushCollection)
{
strPeek += " " + obj.ToString();
}
return strPeek;
}
public string Print_POP()
{
myPushCollection = myStack;
foreach (Object obj in myPushCollection)
{
strPop += " " + obj.ToString();
}
return strPop;
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>mm.Generics4</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="L1" runat="server" Text=""></asp:Label>
<asp:Label ID="L3" runat="server" Text=""></asp:Label>
<asp:Label ID="L2" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var stackString = new StackClass();
L1.Text += "Example of Genercs, Stack.Push-Peek-Pop ";
stackString.Add_Push("string 0");
stackString.Add_Push("string 1");
stackString.Add_Push("string 2");
stackString.Add_Push("string 3");
L1.Text +="<br/><font color='red'> stackString.Print_Push()</font></br>
"+ "---->"+ stackString.Print_Push();
stackString.Peek();
L2.Text += "<br/><font color='red'> stackString.Print_Peek()</font></br>
" + "---->" + stackString.Print_Peek();
stackString.POP();
L2.Text += "<br/><font color='red'> stackString.Print_Pop()</font></br>"
+ "---->" + stackString.Print_POP();
}
}