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