泛型学习



None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Linq;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Xml.Linq;
None.gif
None.gif
namespace test1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    [Serializable]
InBlock.gif    
public class ItemInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
// Internal member variables
InBlock.gif
        private string id;
InBlock.gif        
private string name;
InBlock.gif     
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public ItemInfo() dot.gif{ }
InBlock.gif
InBlock.gif     
InBlock.gif        
public ItemInfo(string id, string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.id = id;
InBlock.gif            
this.name = name;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public string  Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn  id; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gif{return name;}
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="test1._Default" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>Untitled Page</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif    
<div>
None.gif      
None.gif        
<asp:Repeater ID="Repeater1" runat="server">
None.gif        
<ItemTemplate>
None.gif        
<table>
None.gif         
<tr>
None.gif          
ExpandedBlockStart.gifContractedBlock.gif          
<td><%dot.gif#Eval("name"%></td>
None.gif         
</tr>
None.gif        
</table>
None.gif        
</ItemTemplate>
None.gif        
</asp:Repeater>
None.gif      
None.gif    
</div>
None.gif    
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
None.gif    
</form>
None.gif
</body>
None.gif
</html>
None.gif

None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Linq;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Xml.Linq;
None.gif
using System.Collections.Generic;
None.gif
namespace test1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public partial class _Default : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    
InBlock.gif    
InBlock.gif    
InBlock.gif  
static  protected  Dictionary<string,ItemInfo> items=new Dictionary<string,ItemInfo>();
InBlock.gif                     
InBlock.gif                     
InBlock.gif                     
public  void Load_data()
ExpandedSubBlockStart.gifContractedSubBlock.gif                     
dot.gif{            
InBlock.gif                     
for (int i=0;i<5;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                     
dot.gif{
InBlock.gif                        ItemInfo item
=new ItemInfo (i.ToString (),"name"+i.ToString ());
InBlock.gif                        items.Add (item.Id,item);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                       
ExpandedSubBlockEnd.gif                     }

InBlock.gif                     
public void Bind_Data()
ExpandedSubBlockStart.gifContractedSubBlock.gif                      
dot.gif{
InBlock.gif                          
this.Repeater1.DataSource = items.Values;
InBlock.gif                          
this.Repeater1.DataBind();
ExpandedSubBlockEnd.gif                      }

InBlock.gif                     
InBlock.gif                   
InBlock.gif                      
InBlock.gif        
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{                
InBlock.gif        
InBlock.gif                      
if (!this.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif                      
dot.gif{
InBlock.gif                      
this.Load_data();
InBlock.gif                      
this.Bind_Data ();
InBlock.gif                        
InBlock.gif                     
InBlock.gif     
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        items .Remove (
"3");
InBlock.gif        Bind_Data();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif     
InBlock.gif    
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Collections;
None.gif
namespace test1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
public class Stack<T> : IEnumerable<T>
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private T[] values = new T[100];
InBlock.gif        
private int top = 0;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void Push(T t) dot.gif{ values[top++= t; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public T Pop() dot.gifreturn values[--top]; }
InBlock.gif
InBlock.gif        
// These make Stack<T> implement IEnumerable<T> allowing
InBlock.gif        
// a stack to be used in a foreach statement.
InBlock.gif
        public IEnumerator<T> GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = top; -->= 0; )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                yield 
return values[i];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        IEnumerator IEnumerable.GetEnumerator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetEnumerator();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Iterate from top to bottom.
InBlock.gif
        public IEnumerable<T> TopToBottom
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// Since we implement IEnumerable<T>
InBlock.gif                
// and the default iteration is top to bottom,
InBlock.gif                
// just return the object.
InBlock.gif
                return this;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Iterate from bottom to top.
InBlock.gif
        public IEnumerable<T> BottomToTop
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int i = 0; i < top; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    yield 
return values[i];
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//A parameterized iterator that return n items from the top
InBlock.gif
        public IEnumerable<T> TopN(int n)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// in this example we return less than N if necessary 
InBlock.gif
            int j = n >= top ? 0 : top - n;
InBlock.gif
InBlock.gif            
for (int i = top; -->= j; )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                yield 
return values[i];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    }

None.gif


None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Linq;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.WebControls.WebParts;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Xml.Linq;
None.gif
None.gif
namespace test1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public partial class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Stack
<ItemInfo> s = new Stack<ItemInfo>();
InBlock.gif            
for (int i = 0; i < 10; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ItemInfo item 
= new ItemInfo(i.ToString(), "gw" + i.ToString());
InBlock.gif                s.Push(item);
ExpandedSubBlockEnd.gif            }

InBlock.gif          
InBlock.gif            
InBlock.gif          
this.GridView1 .DataSource=s;
InBlock.gif          
this.GridView1 .DataBind ();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Stack
<ItemInfo> s = new Stack<ItemInfo>();
InBlock.gif            
for (int i = 0; i < 10; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ItemInfo item 
= new ItemInfo(i.ToString(), "gw" + i.ToString());
InBlock.gif                s.Push(item);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
this.GridView1.DataSource =
InBlock.gif         s.BottomToTop;
InBlock.gif            
this.GridView1.DataBind();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}



ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test1.WebForm1" %>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
None.gif
<html xmlns="http://www.w3.org/1999/xhtml" >
None.gif
<head runat="server">
None.gif    
<title>Untitled Page</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif    
<div>
None.gif    
None.gif        
<asp:GridView ID="GridView1" runat="server">
None.gif        
</asp:GridView>
None.gif    
None.gif    
</div>
None.gif    
<p>
None.gif        
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
None.gif    
</p>
None.gif    
</form>
None.gif
</body>
None.gif
</html>
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值