首页 > 代码库 > 关于索引器

关于索引器

索引器是一种兼具属性与数组特征的表示法

sealed class PhoneBook	{        private int used;        private Name[] names;        private PhoneNumber[] phoneNumbers;		public PhoneBook()		{			int initialSize = 0;			this.used = 0;			this.names = new Name[initialSize];			this.phoneNumbers = new PhoneNumber[initialSize];		}				public void Add(Name name, PhoneNumber number)		{			enlargeIfFull();			this.names[used] = name;			this.phoneNumbers[used] = number;			this.used++;		}				// write 1st indexer here        public Name this[PhoneNumber number]        {            get            {                int i = Array.IndexOf(this.phoneNumbers, number);                if (i != -1)                {                    return this.names[i];                }                else                {                    return new Name();                }            }        }		// write 2nd indexer here        public PhoneNumber this[Name name]        {            get            {                int i = Array.IndexOf(this.names, name);                if (i != -1)                {                    return this.phoneNumbers[i];                }                else                {                    return new PhoneNumber();                }            }        }		private void enlargeIfFull()		{			if (this.used == this.names.Length)			{				int bigger = used + 16;								Name[] moreNames = new Name[bigger];				this.names.CopyTo(moreNames, 0);								PhoneNumber[] morePhoneNumbers = new PhoneNumber[bigger];				this.phoneNumbers.CopyTo(morePhoneNumbers, 0);										this.names = moreNames;				this.phoneNumbers = morePhoneNumbers;			}		}

  

关于索引器