首页 > 其他 > 详细

c#学习7.索引

时间:2014-05-17 20:11:00      阅读:375      评论:0      收藏:0      [点我收藏+]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 索引
{
class Program
{
static void Main(string[] args)
{
int[] values = { 3, 5, 9, 8 };
int i = values[1];
string s = "sss";
s[0] = "a";
person p1 = new person();
p1[1] = "小明";
Console.WriteLine(p1[1] + p1[2]);
Console.WriteLine(p1["tome", 3, 9]);
//为只读索引,不能赋值
// p1["tome", 3, 9] = "111";
Console.ReadKey();
}
}
class person
{
private string fistname = "打毛";
private string nextname = "xiao毛";
public string this[string name, int a, int b]//string 索引器的数据类型,函数名是 this
{
get
{
return name + a + b;
}
}

public string this[int index]
{
set
{
if (index == 1)
{
fistname = value;
}
else if (index == 2)
{
nextname = value;
}
else
{
throw new Exception("错误");
}
}
get
{
if (index == 1)
{
return fistname;
}
else if (index == 2)
{
return nextname;
}
else
{
throw new Exception("错误");
}

}
}

}
}

c#学习7.索引,布布扣,bubuko.com

c#学习7.索引

原文:http://www.cnblogs.com/cyychenyijie/p/3733199.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!