行易软件

少谈技术,多谈应用

博客园 首页 新随笔 联系 订阅 管理

参考代码:

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"]);
        }
    }
}

 

posted on 2022-11-22 12:07  行易软件技术团队  阅读(5)  评论(0编辑  收藏  举报