天天看點

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#