C#:集合的索引器和索引器的重载

c#定义的集合类型包括Array、List、Queue、Stack等,其中数组类型是唯一一个提供内置支持的集合类型,包括内置索引器,可通过[]实现,支持排序和反序等。
若一个类中包含数组类型,则可为该类创建索引器,已实现该数组的访问。

using System ;
using System . Collections . Generic ;
using System . Linq ;
using System . Text ;
namespace IndexDemo
{
    class ListBoxTest
    {
        //static void Main(string[] args)
        //{
        //}
        private string [] strings ;
        private int cnt = 0 ;
        public ListBoxTest ( params string [] initialstrings )
        {
            strings = new string [ 256 ];
            foreach ( string s in initialstrings )
            {
                strings [ cnt ++] = s ;
            }
        }
        public void Add ( string theString )
        {
            if ( cnt > strings . Length )
            {
                Console . WriteLine ( "the array is out of size." );
            }
            else  
                strings [ cnt ++]= theString ;
        }
        //索引
        public string this[int index]
        {
            get 
            {
                if (index < 0 || index > strings.Length)
                {
                    Console.WriteLine("out of size");
                    return "error";
                }
                else
                    return strings[index];
            }
            set 
            {
                if (index < 0 || index > strings.Length)
                    Console.WriteLine("out of size");
                else
                    strings[index] = value;
            }
        }
        private int findstring ( string searchstring )
        {
            for ( int i = 0 ; i < strings . Length ; i ++)
                if ( strings [ i ]. StartsWith ( searchstring ))
                    return i ;
            return - 1 ;
        }
        //索引器重载
        public string this[string index]
        {
            get
            {
                if (index.Length == 0)
                {
                    Console.WriteLine("index setting error.");
                }
                return this[findstring(index)];
            }
            set
            {
                strings[findstring(index)]=value;
            }
        }
        public int GetNumEntries ()
        {
            return cnt ;
        }
    }
    public class Tester
    {
        static void Main ()
        {
            ListBoxTest mbl = new ListBoxTest ( "hello" , "world" );
            mbl . Add ( "one" );
            mbl . Add ( "two" );
            mbl . Add ( "three" );
            mbl . Add ( "four" );
            mbl [ 3 ] = "test" ;
            mbl [ "th" ]= "override1" ;
            mbl [ "fo" ]= "override2" ;
            for ( int i = 0 ; i < mbl . GetNumEntries (); i ++)
                Console . WriteLine ( "lbt.strings[{0}]={1}" , i , mbl [ i ]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值