首页 > 学院 > 开发设计 > 正文

C#中索引符的理解

2019-11-17 04:00:08
字体:
来源:转载
供稿:网友
      索引符是一种特殊类型的属性,可以把她添加到一个类的定义中,一提供类似于数组的访问。

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

    public class Cards:CollectionBase

     {

         .....

        

       public Card this[int cardIndex]

         {

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

            set{ List[cardIndex]=value; }

         }

     }

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

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

   Cards deckCards = new Cards();

   ....

   当调用deckCards[index]时才能被编译器识别不会报错。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表