C#索引器(Indexer)

C#索引器(Indexer)

using System;

namespace IndexerApplication{

//1.类成员变量是数组的情况

// class IndexedNames{

// private string[] namelist = new string[size];

// static public int size = 10;

// public IndexedNames(){

// for(int i = 0; i < size; i++){

// namelist[i] = "N.A.";

// }

// }

// public string this[int index]{

// get{

// string tmp;

// if(index >=0 && index <=size-1){

// tmp = namelist[index];

// }else{

// tmp="";

// }

// return tmp;

// }

// set{

// if(index >=0 && index <=size-1){

// namelist[index] = value;

// }

// }

// }

// static void Main(string[] args){

// IndexedNames names = new IndexedNames();

// names[0] = "Zara";

// names[1] = "Riz";

// names[2] = "Nuha";

// names[3] = "Asif";

// names[4] = "Davinder";

// names[5] = "Sunil";

// names[6] = "Rubic";

// for(int i = 0; i < IndexedNames.size; i++){

// Console.WriteLine(names[i]);

// }

// }

// }

//2.类成员变量是多个基本类型

class Employee{

public string firstName;

public string middleName;

public string lastName;

public string this[string index]{

set{

switch(index){

case "a":firstName = value;

break;

case "b":middleName = value;

break;

case "c":lastName = value;

break;

default: throw new ArgumentOutOfRangeException("index");

}

}

get{

switch(index){

case "a":return firstName;

case "b":return middleName;

case "c":return lastName;

default: throw new ArgumentOutOfRangeException("index");

}

}

}

static void Main(string[] args){

Employee ee = new Employee();

ee.firstName = "胡";

ee.middleName = "大";

ee.lastName = "阳";

Console.WriteLine("我的名字叫: {0}{1}{2}",ee["a"],ee["b"],ee["c"]);

}

}

}二狗子 二狗子

137***6461@qq.com

7年前 (2018-12-06)

相关任务

365体育网在线手机版 巴士门app下载 3.4.7

巴士门app下载 3.4.7

📅 09-07 👁️ 3707
365速发app下载平台注册 半生江湖,流落龃龉。

半生江湖,流落龃龉。

📅 07-24 👁️ 7078
盒子365app下载 首飞成功 “空中救护车”来了!
365速发app下载平台注册 糖豆人抢尾巴按哪个键

糖豆人抢尾巴按哪个键

📅 07-05 👁️ 3839