GetEnumerator方法

来源:互联网 发布:阿里云栖大会 ppt 编辑:程序博客网 时间:2024/05/19 12:24
 

任何集合类对象都有一个GetEnumerator()方法,该方法可以返回一个实现了 IEnumerator接口的对象,这个返回的IEnumerator对象既不是集合类对象,也不是集合的元素类对象,它是一个独立的类对象。通过这个对象,可以遍历访问集合类对象中的每一个元素对象 .

例说明如何使用 GetEnumerator 方法创建一个实现了 System.Collections.IEnumerator 接口的对象。然后循环访问该对象以显示选定行中的项。
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客view plaincopy to clipboardprint?
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客
<%@ Page Language="C#" AutoEventWireup="True" %>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客  
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客
<HTML>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客 
<HEAD>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
<SCRIPT runat="server">  
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客       
void Page_Load(Object sender, EventArgs e)   
       
{  
            
          
int numrows = 5;  
          
int numcells = 6;  
          
int counter = 1;  
          ArrayList a_row 
= new ArrayList();  
            
          
// Create a table.  
          for (int j=0; j<numrows; j++)   
          
{            
             TableRow r 
= new TableRow();  
             
for (int i=0; i<numcells; i++)   
             
{  
                TableCell c 
= new TableCell();  
                c.Text
=counter.ToString();  
                r.Cells.Add(c);  
                counter
++;  
             }
  
             Table1.Rows.Add(r);  
          }
  
           
          
if (!IsPostBack)   
          
{  
   
             
// Create a DropDownList for the number of rows.  
             for (int k=0; k<numrows; k++)   
             
{  
                a_row.Add(k.ToString());  
             }
  
           
             List1.DataSource
=a_row;   
             List1.DataBind();  
          
          }
  
       }
  
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
void Button_Click(object sender, EventArgs e)   
    
{  
   
       
int row = List1.SelectedIndex;  
       TableCell current_cell;  
   
       
// Create the IEnumerator.  
    IEnumerator myEnum = Table1.Rows[row].Cells.GetEnumerator();        
   
       Label1.Text 
= "The items in the selected row are: ";     
       
// Iterate through the IEnumerator and display its contents.  
       while (myEnum.MoveNext())   
       
{  
            
          current_cell 
= (TableCell)myEnum.Current;  
          Label1.Text 
= Label1.Text + " " + current_cell.Text;  
   
      }
  
               
    }
  
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
</SCRIPT>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
<H3>TableCellCollection Example</H3>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
<FORM runat="server">   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客       
<ASP:TABLE id=Table1 runat="server" />   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客       
<BR><BR>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客       
<CENTER>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客          Select a row:   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客          
<BR><BR>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客          Row:   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客          
<ASP:DROPDOWNLIST id=List1 runat="server" />   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客          
<BR><BR>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客          
<ASP:BUTTON id=Button1 onclick=Button_Click runat="server" Text="Create IEnumerator" />   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客          
<BR><BR>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客          
<ASP:LABEL id=Label1 runat="server" />   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客       
</CENTER>   
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
[自C 中的GetEnumerator方法动保存草稿] - 不是小子 - 我的博客    
</FORM>   

原创粉丝点击