首页 > 代码库 > 关于索引器
关于索引器
索引器是一种兼具属性与数组特征的表示法
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; } }
关于索引器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。