简单的索引,索引时可以重载的
class Program
{
static void Main(string[] args)
{
int[] values = {3,5,9,8};
int i = values[1];//将数据里的数用一个变量接受值
Console.WriteLine(i);
}
}
索引器
class Program
{
static void Main(string[] args)
{
person p = new person();
p[1] = "小明";//将p[1]赋值成小明
Console.WriteLine(p[1] + p[2]);
Console.WeiteLine(p1["hope",2,1])
Console.ReadKey();
}
class person
{
string firstname = "大毛";
string secondname = "二毛";
public string this[stringname, int x,int y]
{
get
{
return name+x+y;
}
}
public string this[int index]//用户传过来的值
{
get
{
if (index == 1)
{
return firstname;
}
else if(index==2)
{
return secondname;
}
else
{
throw new Exception("错误的序号");
}
}
set
{
if (index == 1)
{
firstname=value;
}
else if(index==2)
{
secondname=value;
}
else
{
throw new Exception("错误的序号");
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
int[] values = {3,5,9,8};
int i = values[1];//将数据里的数用一个变量接受值
Console.WriteLine(i);
}
}
索引器
class Program
{
static void Main(string[] args)
{
person p = new person();
p[1] = "小明";//将p[1]赋值成小明
Console.WriteLine(p[1] + p[2]);
Console.WeiteLine(p1["hope",2,1])
Console.ReadKey();
}
class person
{
string firstname = "大毛";
string secondname = "二毛";
public string this[stringname, int x,int y]
{
get
{
return name+x+y;
}
}
public string this[int index]//用户传过来的值
{
get
{
if (index == 1)
{
return firstname;
}
else if(index==2)
{
return secondname;
}
else
{
throw new Exception("错误的序号");
}
}
set
{
if (index == 1)
{
firstname=value;
}
else if(index==2)
{
secondname=value;
}
else
{
throw new Exception("错误的序号");
}
}
}
}
}