日期:2014-05-20  浏览次数:20420 次

请问这样调用的函数怎么写
引用functionname[ "属性名 "]
然后就可以得到值了

------解决方案--------------------
get.........
set............
------解决方案--------------------
是问集合的写法?
------解决方案--------------------
functionname[ "属性名 "]

索引器的使用,
------解决方案--------------------
get
{

}
set
{

}
------解决方案--------------------
当然可以了……给你一个例子。

class DayCollection
{
string[] days = { "Sun ", "Mon ", "Tues ", "Wed ", "Thurs ", "Fri ", "Sat " };

// This method finds the day or returns -1
private int GetDay(string testDay)
{
int i = 0;
foreach (string day in days)
{
if (day == testDay)
{
return i;
}
i++;
}
return -1;
}

// The get accessor returns an integer for a given string
public int this[string day]
{
get
{
return (GetDay(day));
}
}
}

class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
System.Console.WriteLine(week[ "Fri "]);
System.Console.WriteLine(week[ "Made-up Day "]);
}
}