自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#初學(xué)——C#索引(index)

開(kāi)發(fā) 后端
本人通過(guò)學(xué)習(xí),小結(jié)了一下什么是C#索引,以及怎樣聲明C#索引,希望對(duì)大家有用。

通過(guò)C#的一些學(xué)習(xí)內(nèi)容,我看到了C#索引的使用,感覺(jué)自己沒(méi)什么概念,就學(xué)習(xí)了下。感覺(jué)比較好,適合初學(xué)者。

下面是貼出來(lái)的代碼:

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5.    
  6. namespace index   
  7. {   
  8.         class Worker   
  9.         {   
  10.                 public string LastName;   
  11.                 public string FirstName;   
  12.                 public string MyBirth;   
  13.    
  14.                 public string this[int index]   
  15.                 {   
  16.                         set   
  17.                         {   
  18.                                 switch (index)   
  19.                                 {   
  20.                                         case 0: LastName = value;   
  21.                                                 break;   
  22.                                         case 1: FirstName = value;   
  23.                                                 break;   
  24.                                         case 2: MyBirth = value;   
  25.                                                 break;   
  26.                                         default:   
  27.                                                 throw new ArgumentOutOfRangeException("index");   
  28.                                                 break;   
  29.                                 }   
  30.                         }   
  31.                         get   
  32.                         {   
  33.                                 switch(index)   
  34.                                 {   
  35.                                         case 0 : return LastName;   
  36.                                         case 1 : return FirstName;   
  37.                                         case 2 : return MyBirth;   
  38.                                         default :      
  39.                                                 throw new ArgumentOutOfRangeException("index");   
  40.                                                 break;   
  41.                                 }   
  42.                                            
  43.                         }   
  44.                 }   
  45.         }   
  46.         class Program   
  47.         {   
  48.                 static void Main(string[] args)   
  49.                 {   
  50.                         Worker a = new Worker();   
  51.                         Console.WriteLine("print the value:{0},{1},{2}",a[0],a[1],a[2]);   
  52.                         Console.WriteLine("please print your last name");   
  53.                         a[0] = Console.ReadLine();   
  54.                         Console.WriteLine("please print your first name");   
  55.                         a[1] = Console.ReadLine();   
  56.                         Console.WriteLine("please print your birthday");   
  57.                         a[2] = Console.ReadLine();   
  58.                         Console.WriteLine("Now,your name is {0},{1},and your birth is {2}",a[0],a[1],a[2]);   
  59.    
  60.                 }   
  61.         }   
  62. }  

首先什么是C#索引呢?

書(shū)上說(shuō)它是一組get和set訪問(wèn)器,我個(gè)人就直接這么認(rèn)為就是獲值或設(shè)值的概念。(可能是錯(cuò)誤的啊,呵呵,理論太差,剛看的)。

怎樣聲明C#索引呢?

他的語(yǔ)法是如下:

要注意下面幾點(diǎn):a:索引沒(méi)有名稱(chēng),它是通過(guò)關(guān)鍵字this。

                                   b:參數(shù)列表在方括號(hào)里面。

                                   c:參數(shù)列表至少必須聲明一個(gè)參數(shù)。 

  1. ReturnType this [type param1,...]   
  2. {   
  3.         get   
  4.                 {   
  5.                         ...   
  6.                 }   
  7.         set   
  8.                 {   
  9.                         ...   
  10.                 }   
  11. }  

【編輯推薦】

  1. 總結(jié)C#哈希表的用法
  2. 不一樣的入門(mén):看C# Hello World的17種寫(xiě)法
  3. 什么是WMI?及其示例
  4. 從C#到C++容易出現(xiàn)的問(wèn)題解答
  5. 淺議.NET、ASP.NET和C#的關(guān)系
責(zé)任編輯:book05 來(lái)源: 51cto技術(shù)博客
相關(guān)推薦

2009-08-25 17:15:50

C#隱藏C#重寫(xiě)C#重載

2009-08-27 18:05:54

C#索引功能

2009-09-02 17:10:45

C#語(yǔ)言入門(mén)

2009-09-01 17:25:33

初學(xué)C#編程

2009-08-25 17:59:49

C#入門(mén)

2009-08-13 17:04:09

C#語(yǔ)言C#程序

2009-08-27 16:11:03

C# delegateC# event

2009-08-27 12:58:44

C#索引指示器

2009-08-31 17:53:20

C#實(shí)現(xiàn)索引器

2009-08-10 16:40:03

C#索引器

2009-08-19 16:50:32

Visual C#C#語(yǔ)言特性

2009-08-24 09:55:26

C#接口轉(zhuǎn)換

2009-08-18 10:30:30

C#枚舉

2009-08-24 11:02:52

C#接口映射

2009-08-26 10:34:15

C#類(lèi)型C#變量

2016-10-13 13:33:41

反射特性c#

2009-08-28 10:14:45

C#內(nèi)存泄露

2021-01-22 05:53:08

C# IndexRange

2009-08-10 10:04:25

C#抽象類(lèi)C#接口

2009-09-01 17:51:47

C#拆箱C#裝箱
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)