C#编程-96:索引器的使用_彭世瑜_新浪博客

57 阅读1分钟
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5.  

  6. namespace IndexTest

  7. {

  8.     class Clerk

  9.     {

  10.         private string name;

  11.         public string Name

  12.         {

  13.             get { return name; }

  14.             set { name = value; }

  15.         }

  16.         private char gender;

  17.         public char Gender

  18.         {

  19.             get 

  20.             {

  21.                 return gender;

  22.             }

  23.             set {

  24.                 if (value != '男' && value != '女')

  25.                     gender = '男';

  26.                 else

  27.                     gender = value;

  28.             }

  29.         }

  30.         private int[] myint = new int[10];

  31.         //定义索引器

  32.         public int this[int index]

  33.         {

  34.             get { return myint[index]; }

  35.             set { myint[index] = value; }

  36.         }

  37.         //虚索引器

  38.         //public virtual int this[int index]

  39.         //{

  40.         //    get { return myint[index]; }

  41.         //    set { myint[index] = value; }

  42.         //}

  43.         外部索引器

  44.         //public extern int this[int index]

  45.         //{

  46.         //    get;

  47.         //    set;

  48.         //}

  49.     }

  50.  

  51.     //抽象索引器

  52.     abstract class ClerkOther

  53.     {

  54.         public abstract int this[int index]

  55.         {

  56.             set;

  57.             get;

  58.         }

  59.     }

  60.     class Program

  61.     {

  62.         static void Main(string[] args)

  63.         {

  64.         }

  65.     }

  66. }

\

C#编程-96:索引器的使用

C#编程-96:索引器的使用

C#编程-96:索引器的使用
\