通过昨天goody的推荐,到叶子文文上面看了下C#的一些学习内容,感觉比较好,适合初学者。
我简单的过了遍,看到了索引的使用,感觉自己没什么概念,就学习了下。
下面是贴出来的代码:
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Linq;
InBlock.gif using System.Text;
InBlock.gif
InBlock.gif namespace index
InBlock.gif{
InBlock.gif         class Worker
InBlock.gif        {
InBlock.gif                 public string LastName;
InBlock.gif                 public string FirstName;
InBlock.gif                 public string MyBirth;
InBlock.gif
InBlock.gif                 public string this[ int index]
InBlock.gif                {
InBlock.gif                        set
InBlock.gif                        {
InBlock.gif                                 switch (index)
InBlock.gif                                {
InBlock.gif                                         case 0: LastName = value;
InBlock.gif                                                 break;
InBlock.gif                                         case 1: FirstName = value;
InBlock.gif                                                 break;
InBlock.gif                                         case 2: MyBirth = value;
InBlock.gif                                                 break;
InBlock.gif                                         default:
InBlock.gif                                                 throw new ArgumentOutOfRangeException( "index");
InBlock.gif                                                 break;
InBlock.gif                                }
InBlock.gif                        }
InBlock.gif                        get
InBlock.gif                        {
InBlock.gif                                 switch(index)
InBlock.gif                                {
InBlock.gif                                         case 0 : return LastName;
InBlock.gif                                         case 1 : return FirstName;
InBlock.gif                                         case 2 : return MyBirth;
InBlock.gif                                         default :    
InBlock.gif                                                 throw new ArgumentOutOfRangeException( "index");
InBlock.gif                                                 break;
InBlock.gif                                }
InBlock.gif                                        
InBlock.gif                        }
InBlock.gif                }
InBlock.gif        }
InBlock.gif         class Program
InBlock.gif        {
InBlock.gif                 static void Main( string[] args)
InBlock.gif                {
InBlock.gif                        Worker a = new Worker();
InBlock.gif                        Console.WriteLine( "print the value:{0},{1},{2}",a[0],a[1],a[2]);
InBlock.gif                        Console.WriteLine( "please print your last name");
InBlock.gif                        a[0] = Console.ReadLine();
InBlock.gif                        Console.WriteLine( "please print your first name");
InBlock.gif                        a[1] = Console.ReadLine();
InBlock.gif                        Console.WriteLine( "please print your birthday");
InBlock.gif                        a[2] = Console.ReadLine();
InBlock.gif                        Console.WriteLine( "Now,your name is {0},{1},and your birth is {2}",a[0],a[1],a[2]);
InBlock.gif
InBlock.gif                }
InBlock.gif        }
InBlock.gif}
 
首先什么是索引呢?
书上说它是一组get和set访问器,我个人就直接这么认为就是获值或设值的概念。(可能是错误的啊,呵呵,理论太差,刚看的)。
 
怎样声明索引呢?
他的语法是如下:
要注意下面几点:a:索引没有名称,它是通过关键字this。
                                   b:参数列表在方括号里面。
                                   c:参数列表至少必须声明一个参数。
InBlock.gif
InBlock.gifReturnType this [type param1,...]
InBlock.gif{
InBlock.gif        get
InBlock.gif                {
InBlock.gif                        ...
InBlock.gif                }
InBlock.gif        set
InBlock.gif                {
InBlock.gif                        ...
InBlock.gif                }
InBlock.gif}