C#中索引符的理解

作者:互联网

2009-12-04

C#/CSHARP教程

 索引符是一种特殊类型的属性,可以把她添加到一个类的定义中,一提供类似于数组的访问。

  在Card对象的Cards集合中添加索引符:

    public class Cards:CollectionBase

     {

         .....

       

       public Card this[int cardIndex]

         {

            get{ return (Card)List[cardIndex]; }

            set{ List[cardIndex]=value; }

         }

     }

IList.List属性返回的是一个System.Object对象;

简单的理解:在类中建立了索引符后才能调用[Index],例如:

   Cards deckCards = new Cards();

   ....

   当调用deckCards[index]时才能被编译器识别不会报错。
-