泛型学习



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]
    
public class ItemInfo
    
{

        
// Internal member variables
        private string id;
        
private string name;
     

        
public ItemInfo() { }

     
        
public ItemInfo(string id, string name)
        
{
            
this.id = id;
            
this.name = name;
            
        }



        
public string  Id
        
{
            
get return  id; }
        }

        
public string Name
        
{
        
get {return name;}
        }

    

    }

}

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="test1._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>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
      
        
<asp:Repeater ID="Repeater1" runat="server">
        
<ItemTemplate>
        
<table>
         
<tr>
          
          
<td><%#Eval("name"%></td>
         
</tr>
        
</table>
        
</ItemTemplate>
        
</asp:Repeater>
      
    
</div>
    
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    
</form>
</body>
</html>

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
    
{    
    
    
  
static  protected  Dictionary<string,ItemInfo> items=new Dictionary<string,ItemInfo>();
                     
                     
                     
public  void Load_data()
                     
{            
                     
for (int i=0;i<5;i++)
                     
{
                        ItemInfo item
=new ItemInfo (i.ToString (),"name"+i.ToString ());
                        items.Add (item.Id,item);
                        }

                       
                     }

                     
public void Bind_Data()
                      
{
                          
this.Repeater1.DataSource = items.Values;
                          
this.Repeater1.DataBind();
                      }

                     
                   
                      
        
protected void Page_Load(object sender, EventArgs e)
        
{                
        
                      
if (!this.IsPostBack)
                      
{
                      
this.Load_data();
                      
this.Bind_Data ();
                        
                     
     
        }


        }


        
protected void Button1_Click(object sender, EventArgs e)
        
{
        items .Remove (
"3");
        Bind_Data();
        }


     
    
    }

}



using System;
using System.Collections.Generic;
using System.Collections;
namespace test1
{
   
public class Stack<T> : IEnumerable<T>
    
{
        
private T[] values = new T[100];
        
private int top = 0;

        
public void 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; -->= 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.
                return this;
            }

        }


        
// 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; -->= 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
    
{
        
protected void 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 ();

        }


        
protected void 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();

        }

    }

}



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test1.WebForm1" %>

<!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>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
        
<asp:GridView ID="GridView1" runat="server">
        
</asp:GridView>
    
    
</div>
    
<p>
        
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    
</p>
    
</form>
</body>
</html>





posted on 2007-08-13 13:33  gwazy  阅读(292)  评论(0编辑  收藏  举报

导航