您的位置:首页 > 编程语言 > C#

C#索引器1 数字作为索引号

2016-03-08 11:28 246 查看
5.索引器 数字作为索引号

public class IndexerClass

{

private string[] name = new string[2];

public string this[int index]

{

get

{

if (index < 2)

{

return name[index];

}

return null;

}

set

{

if (index < 2)

{

name[index] = value;

}

}

}

}

public class Test

{

public static void Main(string[] args)

{

IndexerClass ic = new IndexerClass();

ic[0] = "sssssss";

ic[1] = "bbbbbbb";

Console.WriteLine("ic[{0}] 为 {1}",0,ic[0]);

Console.WriteLine("ic[{0}] 为 {1}",1,ic[1]);

Console.ReadKey();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: