索引器允許類或者結構的例項按照與陣列相同的方式進行索引取值,索引器與屬性類似,不同的是索引器的訪問是帶參的。
索引器和陣列比較:
(1)索引器的索引值(index)型別不受限制
(2)索引器允許過載
(3)索引器不是乙個變數
索引器和屬性的不同點
(1)屬性以名稱來標識,索引器以函式形式標識
(2)索引器可以被過載,屬性不可以
(3)索引器不能宣告為static,屬性可以
索引器例項:
usingsystem;
using
system.collections;
public
class
indexerclass
return
null
; }
//實現索引器的set方法
set }
}}public
class
test
}
以字串作為下標,對索引器進行訪問
publicclass
indexerclass
}}public
class
test
}
索引器的過載
publicclass
indexerclass
set }
//2:通過values訪問key
public
intthis[string
aname]
}return -1
; }
set }
}public
class
test
}
多參索引器
usingsystem;
using
system.collections;
//入職資訊類
public
class
entrantinfo
public entrantinfo(string name, int num, string
department)
public
string
name
set
}public
intnum
set
}public
string
department
set
}}//
宣告乙個類entrantinfo的索引器
public
class
indexerforentrantinfo
//宣告乙個索引器:以名字和編號查詢訪問部門資訊
public
string
this[string name, int
num]
}return
null
; }
set }
//宣告乙個索引器:以編號查詢名字和部門
public arraylist this[int
num]
}return
temp;}}
//還可以宣告多個版本的索引器...
}public
class
test
}}
C 之索引器
索引器 索引器允許類或結構的例項按照與陣列 相同的方式進行索引。索引器類似於屬性,不同之處在於它們的訪問器採用引數。語法 訪問修飾符 資料型別 this 資料型別 識別符號 set using system using system.collections.generic using system....
C 之 索引器
索引器允許類或者結構的例項按照與陣列相同的方式進行索引取值,索引器與屬性類似,不同的是索引器的訪問是帶參的。索引器和陣列比較 1 索引器的索引值 index 型別不受限制 2 索引器允許過載 3 索引器不是乙個變數 索引器和屬性的不同點 1 屬性以名稱來標識,索引器以函式形式標識 2 索引器可以被過...
C 基礎系列(1)之索引器
索引器允許類或結構的例項就像陣列一樣進行索引。無需顯式指定型別或例項成員,即可設定或檢索索引值。索引器類似於屬性,不同之處在於它們的訪問器需要使用引數。1.需要注意幾點 2.具體實現如下 實現如下 using system using system.collections.generic using...