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

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

    public class Cards:CollectionBase

     {

         .....

        

       public Card this[int cardIndex]

         {

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

            set{ List[cardIndex]=value; }

         }

     }
在访问索引符时,将使用对象名,后跟放在方括号中的索引参数(例如 Cards[0])。

这段代码对List属性使用一个索引符.


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

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

   Cards deckCards = new Cards();

   ....

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

相关文章:

  • 2022-12-23
  • 2022-01-21
  • 2021-05-30
  • 2022-12-23
  • 2021-06-05
  • 2021-05-28
  • 2021-11-23
  • 2021-12-09
猜你喜欢
  • 2021-12-02
  • 2022-01-22
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案