c#学习7.索引

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 索引
{
class Program
{
static void Main(string[] args)
{
int[] values = { 3, 5, 9, 8 };
int i = values[1];
string s = "sss";
s[0] = "a";
person p1 = new person();
p1[1] = "小明";
Console.WriteLine(p1[1] + p1[2]);
Console.WriteLine(p1["tome", 3, 9]);
//为只读索引,不能赋值
// p1["tome", 3, 9] = "111";
Console.ReadKey();
}
}
class person
{
private string fistname = "打毛";
private string nextname = "xiao毛";
public string this[string name, int a, int b]//string 索引器的数据类型,函数名是 this
{
get
{
return name + a + b;
}
}

public string this[int index]
{
set
{
if (index == 1)
{
fistname = value;
}
else if (index == 2)
{
nextname = value;
}
else
{
throw new Exception("错误");
}
}
get
{
if (index == 1)
{
return fistname;
}
else if (index == 2)
{
return nextname;
}
else
{
throw new Exception("错误");
}

}
}

}
}