天天看点

C#初学--index

通过昨天goody的推荐,到叶子文文上面看了下C#的一些学习内容,感觉比较好,适合初学者。 我简单的过了遍,看到了索引的使用,感觉自己没什么概念,就学习了下。 下面是贴出来的代码:

C#初学--index

using System;

C#初学--index

using System.Collections.Generic;

C#初学--index

using System.Linq;

C#初学--index

using System.Text;

C#初学--index
C#初学--index

namespace index

C#初学--index

{

C#初学--index

         class Worker

C#初学--index

        {

C#初学--index

                 public string LastName;

C#初学--index

                 public string FirstName;

C#初学--index

                 public string MyBirth;

C#初学--index
C#初学--index

                 public string this[ int index]

C#初学--index

                {

C#初学--index

                        set

C#初学--index

                        {

C#初学--index

                                 switch (index)

C#初学--index

                                {

C#初学--index

                                         case 0: LastName = value;

C#初学--index

                                                 break;

C#初学--index

                                         case 1: FirstName = value;

C#初学--index

                                                 break;

C#初学--index

                                         case 2: MyBirth = value;

C#初学--index

                                                 break;

C#初学--index

                                         default:

C#初学--index

                                                 throw new ArgumentOutOfRangeException( "index");

C#初学--index

                                                 break;

C#初学--index

                                }

C#初学--index

                        }

C#初学--index

                        get

C#初学--index

                        {

C#初学--index

                                 switch(index)

C#初学--index

                                {

C#初学--index

                                         case 0 : return LastName;

C#初学--index

                                         case 1 : return FirstName;

C#初学--index

                                         case 2 : return MyBirth;

C#初学--index

                                         default :    

C#初学--index

                                                 throw new ArgumentOutOfRangeException( "index");

C#初学--index

                                                 break;

C#初学--index

                                }

C#初学--index
C#初学--index

                        }

C#初学--index

                }

C#初学--index

        }

C#初学--index

         class Program

C#初学--index

        {

C#初学--index

                 static void Main( string[] args)

C#初学--index

                {

C#初学--index

                        Worker a = new Worker();

C#初学--index

                        Console.WriteLine( "print the value:{0},{1},{2}",a[0],a[1],a[2]);

C#初学--index

                        Console.WriteLine( "please print your last name");

C#初学--index

                        a[0] = Console.ReadLine();

C#初学--index

                        Console.WriteLine( "please print your first name");

C#初学--index

                        a[1] = Console.ReadLine();

C#初学--index

                        Console.WriteLine( "please print your birthday");

C#初学--index

                        a[2] = Console.ReadLine();

C#初学--index

                        Console.WriteLine( "Now,your name is {0},{1},and your birth is {2}",a[0],a[1],a[2]);

C#初学--index
C#初学--index

                }

C#初学--index

        }

C#初学--index

}

  首先什么是索引呢? 书上说它是一组get和set访问器,我个人就直接这么认为就是获值或设值的概念。(可能是错误的啊,呵呵,理论太差,刚看的)。   怎样声明索引呢? 他的语法是如下: 要注意下面几点:a:索引没有名称,它是通过关键字this。                                    b:参数列表在方括号里面。                                    c:参数列表至少必须声明一个参数。

C#初学--index
C#初学--index

ReturnType this [type param1,...]

C#初学--index

{

C#初学--index

        get

C#初学--index

                {

C#初学--index

                        ...

C#初学--index

                }

C#初学--index

        set

C#初学--index

                {

C#初学--index

                        ...

C#初学--index

                }

C#初学--index

}

转载于:https://blog.51cto.com/jayai/188731

c#