C#初学——C#索引(index)
通过C#的一些学习内容,我看到了C#索引的使用,感觉自己没什么概念,就学习了下。感觉比较好,适合初学者。

为黄梅等地区用户提供了全套网页设计制作服务,及黄梅网站建设行业解决方案。主营业务为做网站、成都网站设计、黄梅网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
下面是贴出来的代码:
- using System;
 - using System.Collections.Generic;
 - using System.Linq;
 - using System.Text;
 - namespace index
 - {
 - class Worker
 - {
 - public string LastName;
 - public string FirstName;
 - public string MyBirth;
 - public string this[int index]
 - {
 - set
 - {
 - switch (index)
 - {
 - case 0: LastName = value;
 - break;
 - case 1: FirstName = value;
 - break;
 - case 2: MyBirth = value;
 - break;
 - default:
 - throw new ArgumentOutOfRangeException("index");
 - break;
 - }
 - }
 - get
 - {
 - switch(index)
 - {
 - case 0 : return LastName;
 - case 1 : return FirstName;
 - case 2 : return MyBirth;
 - default :
 - throw new ArgumentOutOfRangeException("index");
 - break;
 - }
 - }
 - }
 - }
 - class Program
 - {
 - static void Main(string[] args)
 - {
 - Worker a = new Worker();
 - Console.WriteLine("print the value:{0},{1},{2}",a[0],a[1],a[2]);
 - Console.WriteLine("please print your last name");
 - a[0] = Console.ReadLine();
 - Console.WriteLine("please print your first name");
 - a[1] = Console.ReadLine();
 - Console.WriteLine("please print your birthday");
 - a[2] = Console.ReadLine();
 - Console.WriteLine("Now,your name is {0},{1},and your birth is {2}",a[0],a[1],a[2]);
 - }
 - }
 - }
 
首先什么是C#索引呢?
书上说它是一组get和set访问器,我个人就直接这么认为就是获值或设值的概念。(可能是错误的啊,呵呵,理论太差,刚看的)。
怎样声明C#索引呢?
他的语法是如下:
要注意下面几点:a:索引没有名称,它是通过关键字this。
b:参数列表在方括号里面。
c:参数列表至少必须声明一个参数。
- ReturnType this [type param1,...]
 - {
 - get
 - {
 - ...
 - }
 - set
 - {
 - ...
 - }
 - }
 
文章标题:C#初学——C#索引(index)
当前链接:http://jxruijie.cn/article/dhceeod.html
