码迷,mamicode.com
首页 > Windows程序 > 详细

C# 属性和索引

时间:2015-01-16 22:16:08      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:

//用索引取一个记录中的各项

using system;

class IndexerRecord{

private string[] data= new string [6];

private string[] keys = {

     "Author""Publisher""Title",

      "Subject""ISBN""Comments" 

       };

//程序中用了两种方法索引,一是整数作下标,一是关键字(字符串)作下标

public string this [int idx]{

     set

    {

        if( idx>=0 && idx<data.length )

            data[idx]=value;

   }

   get

  {

     if(idx>=0 && idx <data.length)

       return data[idx];

     return NULL;

  }

}

 public string this[string key]

{

    set

    {

       ind idx = FindKey(key);

       this[idx] = value;

    }

   get

   {

      return this[FindKey(key)];

   }

}

  private int FindKey(string key)

{

      for(int i=0;i<keys.length;i++)

         if(keys[i] == key)return i;

     return -1;

 }

 static void Main()

{

     IndexRecord record = new IndexRecord();

     record[0] = "马克 吐温";

     record[1] = "Crox出版公司";

     record[2] = "汤姆 索亚历险记";

     Console.WriteLine(record["Title"]);

     Console.WriteLine( record[ "Author" ] );

     Console.WriteLine( record[ "Publisher" ] );

}

}

 

//属性

class Person

{

   public string Name{set;get;}

}

 

C# 属性和索引

标签:

原文地址:http://www.cnblogs.com/573177885qq/p/4229774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!