可以定义多个索引,类型不同就行 public class A { public string this[int index] { get { return index.ToString(); } } public string this[string name] { get { return name; } } }
根据参数个数 例如: public int this[int index] { get { // code here} set { // code here} } public int this[int index,int index] { get { // code here} set { // code here} } public int this[string index] { get { // code here} set { // code here} } public int this[string index,string index] { get { // code here} set { // code here} } 等等,这就是索引器重载 与返回类型 没关系 只与 参数个数和参数类型有关 再次索引器没有名称
public class A
{
public string this[int index]
{
get { return index.ToString(); }
}
public string this[string name]
{ get { return name; }
}
}
例如:
public int this[int index]
{
get
{ // code here}
set
{ // code here}
}
public int this[int index,int index]
{
get
{ // code here}
set
{ // code here}
}
public int this[string index]
{
get
{ // code here}
set
{ // code here}
}
public int this[string index,string index]
{
get
{ // code here}
set
{ // code here}
}
等等,这就是索引器重载 与返回类型 没关系 只与 参数个数和参数类型有关 再次索引器没有名称