C#語法學習(索引器[indexer])

/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 09:44
 * 索引器(indexer)[也被稱為有參屬性]
 * 索引器允許類或結構的實例按照與數組相同的方式進行索引.
 * 索引器類似於屬性,不同之處在於它們的訪問器采用參數.
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
 
using System;
 
class ArrClass//沒有索引用器的類
 {
     
private readonly string name;
     
public ArrClass(string name)
     {
         
this.name=name;
     }
     
public string Name
     {
         
get{return name;}
     }
 }

 
class Test
 {
     
static void Main()
     {
         
//數組類的使用
         ArrClass[] a=new ArrClass[10];
         a[
0]=new ArrClass("張三");
         a[
1]=new ArrClass("李四");
         a[
2]=new ArrClass("王五");
         Console.WriteLine(
"a[0]=" + a[0].Name);
         Console.WriteLine(
"a[1]=" + a[1].Name);
         Console.WriteLine(
"a[2]=" + a[2].Name);
     }
 }
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 09:44
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
 
using System;
 
class ArrClass//沒有索引用器的類
 {
     
private readonly string name;
     
public ArrClass(string name)
     {
         
this.name=name;
     }
     
public string Name
     {
         
get{return name;}
     }
 }
class IndexClass//帶索引器的類,把數組封裝到類里的一种方法
{
    
private string[] name=new string[10];
    
public string this[int index]//定議索引器,索引器的索引值不一定為整數,還可以是其它類型
    {
        
get
        {
            
return name[index];
        }
        
set
        {
            name[index]
=value;
        }
    }
}
 
class Test
 {
     
static void Main()
     {
         
//數組類的使用
         ArrClass[] a=new ArrClass[10];
         a[
0]=new ArrClass("張三");
         a[
1]=new ArrClass("李四");
         a[
2]=new ArrClass("王五");
         Console.WriteLine(
"a[0]=" + a[0].Name);
         Console.WriteLine(
"a[1]=" + a[1].Name);
         Console.WriteLine(
"a[2]=" + a[2].Name);
         
//索引器的使用
         IndexClass b=new IndexClass();
         b[
0]="張三";
         b[
1]="李四";
         b[
2]="王五";
         Console.WriteLine(
"b[0]=" + b[0]);
         Console.WriteLine(
"b[1]=" + b[1]);
         Console.WriteLine(
"b[2]=" + b[2]);
     }
 }
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 09:44
 * 定議索引器,索引器的索引值不一定為整數,還可以是其它類型
 * Hashtable
 * 在.net framework中,hashtable是system.collections命名空間提供的一個容器
 * 用於處理和表現類似key/value的鍵值對.
 * 其中key通常可以用來快速查找,同時key是區分大小寫;
 * value用於存儲對應於key的值.
 * hashtable中key/value鍵值對均為object類型,所以hashtable可以支持任何類型的
 * key/value鍵值對.
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
 
using System;
 
using System.Collections;
class IndexClass//帶索引器的類,把數組封裝到類里的一种方法
{
    
private Hashtable name=new Hashtable();
    
public string this[string index]//定議索引器,索引器的索引值不一定為整數,還可以是其它類型
    {
        
get
        {
            
//因為hashtable返回的是一個object類型需要轉換成string;
            return name[index].ToString();
        }
        
set
        {
            
//給hashtable賦值時是key,value的鍵值對的形式
            name.Add(index,value);
        }
    }
}
 
class Test
 {
     
static void Main()
     {

         
//索引器的使用
         IndexClass b=new IndexClass();
         b[
"A001"]="張三";
         b[
"A002"]="李四";
         b[
"A003"]="王五";
         Console.WriteLine(
"b[A001]=" + b["A001"]);
         Console.WriteLine(
"b[A002]=" + b["A002"]);
         Console.WriteLine(
"b[A003]=" + b["A003"]);
     }
 }
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 09:44
 * 索引器可以被重載
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
 
using System;
 
using System.Collections;
class IndexClass//帶索引器的類,把數組封裝到類里的一种方法
{
    
private Hashtable name=new Hashtable();
    
public string this[int index]//定議索引器,索引器的索引值不一定為整數,還可以是其它類型//A索引器
    {
        
get
        {
            
//因為hashtable返回的是一個object類型需要轉換成string;
            return name[index].ToString();
        }
        
set
        {
            
//給hashtable賦值時是key,value的鍵值對的形式
            name.Add(index,value);
        }
    }
    
public int this[string aName]//B索引器
    {
        
get
        {
            
foreach(DictionaryEntry d in name)
            {
                
if(d.Value.ToString()==aName)
                {
                    
return Convert.ToInt32(d.Key);
                }
            }
            
return -1;
        }
        
set{name.Add(value,aName);}
    }
}
 
class Test
 {
     
static void Main()
     {

         
//索引器的使用
         IndexClass b=new IndexClass();
         
//調用A索引器
         b[100]="張三";
         b[
200]="李四";
         b[
300]="王五";
         Console.WriteLine(
"編號為100的員工是:" + b[100]);
         Console.WriteLine(
"編號為200的員工是:" + b[200]);
         Console.WriteLine(
"編號為300的員工是:" + b[300]);
         
//調用B索引器
         Console.WriteLine("張三的編號是:" + b["張三"]);
         Console.WriteLine(
"李四的編號是:" + b["李四"]);
         Console.WriteLine(
"王五的編號是:" + b["王五"]);
         b[
"馬六"]=400;
         b[
"錢七"]=500;
         
//調用A索引器
         Console.WriteLine("編號為400的員工是:" + b[400]);
         Console.WriteLine(
"編號為500的員工是:" + b[500]);         
         
//Console.WriteLine("馬六的編號是:" + b["馬六"]);
         
//Console.WriteLine("錢七的編號是:" + b["錢七"]);
     }
 }
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/27
 * Time: 下午 10:23
 * 索引器與數組的比較
 * 1,索引器的索引值(Index)類型不受限為整數
 * 用來訪問數組的索引值(index),其類型一定為整數,然後索引器可以定議其他類型的索引值.
 * 2,索引器允許重載(Overloading)
 * 一個類並不限制只能夠定議一下索引器,只要索引器的函數簽名不同,一個類就可以擁有很多個索引器,你可以重載它的功能.
 * 3,索引器不是一個變量
 * 索引和數組不同的地方在於,索引器並沒有直接對應應用數據存儲的地方,而數組則有.
 * 索引器有get訪問器與set訪問器,用來指明要讀取或是寫入索引器元素時,需要執行的代碼.
 * 
 * 
 * 索引器與屬性的不同點
 * 1,標識方式:屬性以名稱來標識;索引器則以函數簽名來票識.
 * 2,索引器可以被重載:因為屬性是以名稱來標識的,所以不能被重載;索引器是以函數簽名來標識的,因此可以重載.
 * 3,索引器不可以聲明為static:屬性可以為static,而索引器永遠屬於實例成員,不能為static.
 * 
 * 
 * 索引器可以用多個參數來訪問
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
using System;
using System.Windows.Forms;
using System.Collections;
//姓名,課程id,成績
class CourseScore//課程的分數類
{
    
private string name;
    
private int courseID;
    
private int score;
    
public CourseScore(string name,int courseID,int score)
    {
        
this.name=name;
        
this.courseID=courseID;
        
this.score=score;
    }
    
public string Name
    {
        
get{return name;}
        
set{name=value;}
    }
    
public int CourseID
    {
        
get{return courseID;}
        
set{courseID=value;}
    }
    
public int Score
    {
        
get{return score;}
        
set{score=value;}
    }
}
class CourseScoreIndexer//索引器用於存取和查詢CourseScore類
{

    
private ArrayList arrCourseScore;
    
public CourseScoreIndexer()
    {
        arrCourseScore
=new ArrayList();
    }
    
public int this[string name,int courseID]
    {
        
get
        {
            
foreach(CourseScore cs in arrCourseScore)
            {
                
if(cs.Name==name && cs.CourseID==courseID)
                {
                    
return cs.Score;
                }
            }
            
return -1;
        }
        
set
        {
            arrCourseScore.Add(
new CourseScore(name,courseID,value));//arr["張三",1]=90
        }
    }
    
public ArrayList this[string name]
    {
        
get
        {
            ArrayList tempArr
=new ArrayList();
            
foreach(CourseScore cs in arrCourseScore)
            {
                
if(cs.Name==name)
                {
                    tempArr.Add(cs);
                }
            }
            
return tempArr;
        }
    }
}
class Test
{
    
static void Main()
    {
        CourseScoreIndexer csi
=new CourseScoreIndexer();
        csi[
"張三",1]=90;
        csi[
"張三",2]=85;
        csi[
"張三",3]=80;
        csi[
"李四",1]=70;
        Console.WriteLine(csi[
"張三",2]);
        Console.WriteLine(
"返回張三的所有成績");
        ArrayList tempArr;
        tempArr
=csi["張三"];
        
foreach(CourseScore cs in tempArr)
        {
            Console.WriteLine(
"姓名:" + cs.Name + " 課程編號:" + cs.CourseID + " 分數:" + cs.Score);
        }
    }
}

申明

非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

博文欢迎转载,但请给出原文连接。

原文地址:https://www.cnblogs.com/Athrun/p/1278199.html