C#中的索引器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace 索引器
{
    class Program
    {
        static void Main(string[] args)
        {
            MyIndexer myindex = new MyIndexer();
            myindex[0] = 23;
            myindex[3] = 2;
            for (int i = 0; i < myindex.Length(); i++)
            {
                Console.WriteLine("arr[{0}]={1}", i, myindex[i]);

            }

    //对象数组
            MyIndexer []index1 = new MyIndexer[5];
            for (int i = 0; i < index1.Length; i++)
            {
                index1[i] = new MyIndexer();
            }
            index1[1][0] = 20;
            Console.WriteLine(index1[1][0]);

        }
    }
    class MyIndexer
    {
        private int[] arr = new int[5];
        public int this[int index]
        {
            get 
            {
                if (index >= 0 && index <= 4) return arr[index];
                else return 0;
            }
            set
            {
                if (index >= 0 && index <= 4) arr[index] = value;
            }
        }
        public int Length()
        { 
             return arr.Length;
        }
    }

}

//使用索引器访问离散字段

namespace 索引器
{
    class Program
    {
        static void Main(string[] args)
        {
            PhoneBook phoneBook = new PhoneBook("张彬");
            phoneBook["officephone"] = "officephone123";
            phoneBook["homephone"] = "homePhone23";
            phoneBook["mobilePhone"] = "mobilephone12";
            phoneBook.PrintPhoneBook();
        }
    }
    //使用索引器访问离散字段
    class PhoneBook
    {
        private string name;
        private string officePhone;
        private string homePhone;
        private string mobilePhone;
        public PhoneBook() { }
        public PhoneBook(string name)
        {
            this.name = name;
        }
        public string Name
        {
            get 
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string this[string type]
        {
            get 
            {
                switch (type.ToLower())
                {
                    case "officephone": return officePhone;
                    case "homephone": return homePhone;
                    case "mobilephone": return mobilePhone;
                    default: return null;
                }            
            }
            set
            {
                switch (type.ToLower())
                {
                    case "officephone": officePhone = value; break;
                    case "homephone": homePhone = value; break;
                    case "mobilephone": mobilePhone = value; break;
                    default: break;
                }            
            }
        }
        public void PrintPhoneBook()
        {
            Console.WriteLine(name);
            Console.WriteLine("办公电话{0}",officePhone);
            Console.WriteLine("家庭电话{0}",homePhone);
            Console.WriteLine("私人电话{0}",mobilePhone);
        }
  
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值