参考代码:
using System; namespace IndexerDemo { class StuInfo { public string Name; public string[] CouNameArray = new string[3]; public decimal[] CouSocreArray = new decimal[3]; public string this[int Index] { get { return CouNameArray[Index]; } } public decimal this[int Index,string CouName] { get { if (Index != -1) return CouSocreArray[Index]; else if (!string.IsNullOrEmpty(CouName)) return CouSocreArray[Array.IndexOf(CouNameArray, CouName)]; else return -1; } set { CouNameArray[Index] = CouName; CouSocreArray[Index] = value; } } } class Program { static void Main(string[] args) { StuInfo stuInfo = new StuInfo(); stuInfo.Name = "张三"; stuInfo[0, "Math"] = 99; stuInfo[1, "Chinese"] =100; stuInfo[2, "CSharp"] = 120; for (int i = 0; i < 3; i++) { Console.WriteLine(stuInfo[i,""]); } Console.WriteLine(stuInfo[2]); Console.WriteLine(stuInfo[-1, "CSharp"]); } } }
标签:Index,return,string,指示器,CouSocreArray,索引,stuInfo,public From: https://www.cnblogs.com/exesoft/p/16914733.html