码迷,mamicode.com
首页 > 其他好文 > 详细

c#学习7.索引

时间:2014-05-17 20:10:36      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:style   class   c   ext   color   int   

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.索引

标签:style   class   c   ext   color   int   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!