A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© c19t043 中级黑马   /  2014-7-5 22:49  /  1014 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 c19t043 于 2014-7-8 18:30 编辑

----类中只有1个数组时索引的用法   
class 索引
    {
        private string[] strNAS = { "姓名", "年龄", "性别" };
        //private string[] strHHW = { "爱好", "身高", "体重" };
        public string this[int index]
        {
            get
            {
                return strNAS[index];
            }
            set
            {
                strNAS[index] = value;
            }
        }
        public int Length
        {
            get
            {
                return strNAS.Length;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            索引 nas = new 索引();
            for (int i = 0; i <= nas.Length - 1; i++)
            {
                Console.WriteLine(nas);//<<<-------------------------------nas
            }
            Console.ReadKey();
        }
    }
----------------'索引'类中有2个数组时,还是这么用?

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

2 个回复

倒序浏览
// Using a string as an indexer value
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"]);
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
输出
5
-1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
楼上,这样写,
通过索引器,查找,关键字,在数组中的索引
------------这个问题,今天早上突然明白的
我的理解
用索引器的目的,通过 索引,快速调用数组中的值,而不用 类。数组明+加索引
如果,类中有2个数组,用索引器,感觉就有点,不伦不类了

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马