C#索引器的使用
来源:互联网 发布:全国软件测试大赛 编辑:程序博客网 时间:2024/05/16 14:40
索引器可以像数组一样的使用,在使用的时候需要先定义,定义好之后只需要创建一个对象就可以了。
也就是说你可以使用一个对象进行不同的操作。也就是将一个实例对象划分成更小的部分了。
索引器也可以进行重载
using System;
namespace IndexerApplication
{
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";
names[9] = "stywen";
for (int i = 0; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
}
也就是说你可以使用一个对象进行不同的操作。也就是将一个实例对象划分成更小的部分了。
索引器也可以进行重载
using System;
namespace IndexerApplication
{
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";
names[9] = "stywen";
for (int i = 0; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
}
0 0
- C#索引器的使用
- c#索引器的使用
- C#索引器的使用
- C# 索引器的使用
- C#索引器使用
- c#中索引器的使用
- c#中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C#类中索引器的使用
- C#的索引器
- C#的索引器
- C#索引器-索引器的定义
- C#索引器-索引器的定义
- GRE写作必备句型
- 真正有用的 返回键 重写 退出软键盘 或回到主界面
- java 堆和栈
- 【C++】利用Visual Studio 2008编写C++,Visual Studio 2008的基本使用
- bestcoder48 第二题
- C#索引器的使用
- mic in和line-in的区别
- Multithreaded simple data type access and atomic variables
- VS2008下编译生成exe的设置
- HDU4979 A simple math problem.
- 纯CSS3美化radio和checkbox
- POJ2964日历问题
- 2025 查找最大元素
- 使用URLDecoder和URLEncoder对中文进行处理