标签:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.InteropServices; 5 using System.Text; 6 7 namespace ClassTrining 8 { 9 class Demo 10 { 11 //===============属性============================================= 12 //声明私有字段 13 private string _name = "Mike"; 14 /// <summary> 15 /// 声明属性 16 /// </summary> 17 public string Name 18 { 19 get 20 { 21 //返回声明的私有字段值 22 return _name; 23 } 24 set 25 { 26 //用字段来保存用户设置的值 27 _name = value; 28 } 29 } 30 //===============属性============================================= 31 32 33 //===============索引器============================================= 34 //声明字符串数组 35 string[] strArray = new string[100]; 36 /// <summary> 37 /// 声明字符串类型 索引器 this 用于操作字符串数组,通过索引来找值 38 /// </summary> 39 /// <param name="i"></param> 40 /// <returns></returns> 41 public string this[int i] 42 { 43 get 44 { 45 //返回数组的指定索引值 46 return strArray[i]; 47 } 48 set 49 { 50 //用数组指定索引位置改变值与保存用户设置值 51 strArray[i] = value; 52 } 53 } 54 //===============索引器============================================= 55 56 } 57 58 59 class Mijk 60 { 61 private string name = "LiHong"; 62 63 64 public string Name 65 { 66 get 67 { 68 return name; 69 } 70 set 71 { 72 name = value; 73 } 74 } 75 76 private string[] meli = new string[100]; 77 78 79 public string this[int i] 80 { 81 get 82 { 83 return meli[i]; 84 } 85 set 86 { 87 meli[i] = value; 88 } 89 } 90 91 public void Test() 92 { 93 meli[0] = "Hello"; 94 meli[1] = "Kity"; 95 meli[2] = "csharp"; 96 meli[12] = "studio"; 97 98 string str = meli[2]; 99 100 101 meli[99] = "NOKIA"; 102 103 string nokia = meli[99]; 104 } 105 106 107 108 109 110 } 111 112 113 114 }
标签:
原文地址:http://www.cnblogs.com/linhongquan/p/5470647.html