using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; namespace test1 { [Serializable] publicclass ItemInfo { // Internal member variables privatestring id; privatestring name; public ItemInfo() { } public ItemInfo(string id, string name) { this.id = id; this.name = name; } publicstring Id { get{ return id; } } publicstring Name { get{return name;} } } }
using System; using System.Data; using System.Configuration; using System.Collections; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; using System.Collections.Generic; namespace test1 { public partial class _Default : System.Web.UI.Page { staticprotected Dictionary<string,ItemInfo> items=new Dictionary<string,ItemInfo>(); publicvoid Load_data() { for (int i=0;i<5;i++) { ItemInfo item=new ItemInfo (i.ToString (),"name"+i.ToString ()); items.Add (item.Id,item); } } publicvoid Bind_Data() { this.Repeater1.DataSource = items.Values; this.Repeater1.DataBind(); } protectedvoid Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.Load_data(); this.Bind_Data (); } } protectedvoid Button1_Click(object sender, EventArgs e) { items .Remove ("3"); Bind_Data(); } } }
using System; using System.Collections.Generic; using System.Collections; namespace test1 { publicclass Stack<T> : IEnumerable<T> { private T[] values =new T[100]; privateint top =0; publicvoid Push(T t) { values[top++] = t; } public T Pop() { return values[--top]; } // These make Stack<T> implement IEnumerable<T> allowing // a stack to be used in a foreach statement. public IEnumerator<T> GetEnumerator() { for (int i = top; --i >=0; ) { yield return values[i]; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } // Iterate from top to bottom. public IEnumerable<T> TopToBottom { get { // Since we implement IEnumerable<T> // and the default iteration is top to bottom, // just return the object. returnthis; } } // Iterate from bottom to top. public IEnumerable<T> BottomToTop { get { for (int i =0; i < top; i++) { yield return values[i]; } } } //A parameterized iterator that return n items from the top public IEnumerable<T> TopN(int n) { // in this example we return less than N if necessary int j = n >= top ?0 : top - n; for (int i = top; --i >= j; ) { yield return values[i]; } } } }
using System; using System.Data; using System.Configuration; using System.Collections; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; namespace test1 { public partial class WebForm1 : System.Web.UI.Page { protectedvoid Page_Load(object sender, EventArgs e) { Stack<ItemInfo> s =new Stack<ItemInfo>(); for (int i =0; i <10; i++) { ItemInfo item =new ItemInfo(i.ToString(), "gw"+ i.ToString()); s.Push(item); } this.GridView1 .DataSource=s; this.GridView1 .DataBind (); } protectedvoid Button1_Click(object sender, EventArgs e) { Stack<ItemInfo> s =new Stack<ItemInfo>(); for (int i =0; i <10; i++) { ItemInfo item =new ItemInfo(i.ToString(), "gw"+ i.ToString()); s.Push(item); } this.GridView1.DataSource = s.BottomToTop; this.GridView1.DataBind(); } } }