新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#初學——C#索引(index)
通過C#的一些學習內(nèi)容,我看到了C#索引的使用,感覺自己沒什么概念,就學習了下。感覺比較好,適合初學者。

為黃梅等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及黃梅網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為做網(wǎng)站、成都網(wǎng)站設(shè)計、黃梅網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
下面是貼出來的代碼:
- 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訪問器,我個人就直接這么認為就是獲值或設(shè)值的概念。(可能是錯誤的啊,呵呵,理論太差,剛看的)。
怎樣聲明C#索引呢?
他的語法是如下:
要注意下面幾點:a:索引沒有名稱,它是通過關(guān)鍵字this。
b:參數(shù)列表在方括號里面。
c:參數(shù)列表至少必須聲明一個參數(shù)。
- ReturnType this [type param1,...]
- {
- get
- {
- ...
- }
- set
- {
- ...
- }
- }
標題名稱:C#初學——C#索引(index)
轉(zhuǎn)載源于:http://m.5511xx.com/article/dhceeod.html


咨詢
建站咨詢
