手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#索引器简单实例代码
C#索引器简单实例代码
摘要:复制代码代码如下:publicclassFruit{stringpeach="aroundjuicyfruitthathasasoftyel...

复制代码 代码如下:

public class Fruit

{

string peach = "a round juicy fruit that has a soft yellow or red skin and a large hard seed in the center, or the tree that this fruit grows on";

string orange = "a round fruit that has a thick orange skin and is divided into parts inside";

string banana = "a long curved tropical fruit with a yellow skin";

string apple = "a hard round fruit that has red, light green, or yellow skin and is white inside ";

public string this[string fruitName]

{

get

{

switch (fruitName)

{

case "peach":

return peach;

case "orange":

return orange;

case "banana":

return banana;

case "apple":

return apple;

default:

throw new Exception("wrong fruit name");

}

}

set

{

switch (fruitName)

{

case "peach":

peach = value;

break;

case "orange":

orange = value;

break;

case "banana":

banana = value;

break;

case "apple":

apple = value;

break;

default:

throw new Exception("wrong fruit name");

}

}

}

}

class Program

{

static void Main(string[] args)

{

Fruit f = new Fruit();

//关联数组的方式访问get方法

Console.WriteLine(f["peach"]);

//关联数组的方式访问set方法

f["peach"] = "I like to eat peach.";

Console.WriteLine(f["peach"]);

Console.ReadLine();

}

}

【C#索引器简单实例代码】相关文章:

C#控制台输出进度和百分比的实例代码

C# 系统热键注册实现代码

C#自动创建数据库实现代码

C# 邮件发送和接收实现代码

C#打印出正等腰三角形实例代码

C# 注册表 操作实现代码

c# 引用类型与值类型的区别详解

C#用Activex实现Web客户端读取RFID功能的代码

C# 中文简体转繁体实现代码

C#版ftp方法实现类的代码

精品推荐
分类导航