CollectionBase的使用方法

程序中我们经常使用集合这一个概念,一般情况下我们会使用一个数组来完成类似的功能,其实我们可以使用CollectionBase类来解决类似的问题,请看下面的实例代码。首先,我们来写一个类,它继承了CollectionBase类

using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

 

namespace ConsoleApplication1

{

     class Int16Collection:System.Collections.CollectionBase

     {

         public Int16 this[int index]//建立一个索引

         {

              get

              {

                   return ((Int16)List[index]);

              }

              set

              {

                   List[index] = value;

              }

         }

 

         public int Add(Int16 value)

         {

              return (List.Add(value));

         }

 

         public int IndexOf(Int16 value)

         {

              return (List.IndexOf(value));

         }

 

         public void Insert(int index, Int16 value)

         {

              List.Insert(index, value);

         }

 

         public void Remove(Int16 value)

         {

              List.Remove(value);

         }

 

         public bool Contains(Int16 value)

         {

              // If value is not of type Int16, this will return false.

              return (List.Contains(value));

         }

这一方法在(List.Add(value)) 时被调用    

         protected override void OnInsert(int index, Object value)

 {

              // Insert additional code to be run only when inserting values.

              Console.WriteLine("");

          }

//这一方法在List. Remove (int index, Object value)时被首先调用

 

         protected override void OnRemove(int index, Object value)

         {

              // Insert additional code to be run only when removing values.

         }

 

         protected override void OnSet(int index, Object oldValue, Object newValue)

         {

              // Insert additional code to be run only when setting values.

         }

//这一方法在List.Add(value)时被首先调用

         protected override void OnValidate(Object value)

         {

              if (value.GetType() != Type.GetType("System.Int16"))

                   throw new ArgumentException("value must be of type Int16.", "value");

         }

 

     }

}

在主函数中生成该类的实例代码如下:

 

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplication1

{

     class Program

     {

         static void Main(string[] args)

         {

              // Creates and initializes a new CollectionBase.

              Int16Collection myI16 = new Int16Collection();

 

              // Adds elements to the collection.

              myI16.Add((Int16)1);

              myI16.Add((Int16)2);

              myI16.Add((Int16)3);

              myI16.Add((Int16)5);

              myI16.Add((Int16)7);

 

              // Displays the contents of the collection using the enumerator.

              Console.WriteLine("Initial contents of the collection:");

              PrintIndexAndValues(myI16);

 

              // Searches the collection with Contains and IndexOf.

              Console.WriteLine("Contains 3: {0}", myI16.Contains(3));

              Console.WriteLine("2 is at index {0}.", myI16.IndexOf(2));

              Console.WriteLine();

 

              // Inserts an element into the collection at index 3.

              myI16.Insert(3, (Int16)13);

              Console.WriteLine("Contents of the collection after inserting at index 3:");

              PrintIndexAndValues(myI16);

 

              // Gets and sets an element using the index.

              myI16[4] = 123;

              Console.WriteLine("Contents of the collection after setting the element at index 4 to 123:");

              PrintIndexAndValues(myI16);

 

              // Removes an element from the collection.

              myI16.Remove((Int16)2);

 

              // Displays the contents of the collection using the index.

              Console.WriteLine("Contents of the collection after removing the element 2:");

              for (int i = 0; i < myI16.Count; i++)

              {

                   Console.WriteLine("   [{0}]:   {1}", i, myI16[i]);

              }

 

         }

        

         public static void PrintIndexAndValues(Int16Collection myCol)

         {

              int i = 0;

              System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();

              while (myEnumerator.MoveNext())

                   Console.WriteLine("[{0}]:   {1}", i++, myEnumerator.Current);

              Console.WriteLine();

         }

     }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值