Chinaunix首页 | 论坛 | 博客
  • 博客访问: 648916
  • 博文数量: 107
  • 博客积分: 4135
  • 博客等级: 上校
  • 技术积分: 1182
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-06 16:01
文章分类

全部博文(107)

文章存档

2020年(2)

2012年(5)

2011年(6)

2010年(23)

2009年(17)

2008年(35)

2007年(19)

我的朋友

分类: C/C++

2009-09-29 09:54:15

 
C#中有索引器的概念,在MSDN或一些书籍中关于索引器总有类似描述:索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。
  • 索引器使得对象可按照与数组相似的方法进行索引。
  • get 访问器返回值。set 访问器分配值。
  • this 关键字用于定义索引器。
  • value 关键字用于定义由 set 索引器分配的值。
  • 索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
  • 索引器可被重载。
  • 索引器可以有多个形参,例如当访问二维数组时。

作为初学进看到上述描述时可能还是比较疑惑其作用与用法,其实在理解属性后再理解索引器相对较容易。MSDN中关于属性的解释是这样的:属性是这样的成员:它们提供灵活的机制来读取、编写或计算私有字段的值。可以像使用公共数据成员一样使用属性,但实际上它们是称为“访问器”的特殊方法。这使得数据在可被轻松访问的同时,仍能提供方法的安全性和灵活性。

下面的例子定义一个Person类,其中私有字段name和age的读写就是通过属性来完成的,其实Age属性还可以完成取值过滤功能:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Demo
{
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
 
private int age;
public int Age
{
get { return age; }
set
{
if (age < 0 || age > 100)
age = 0;
else
age = value;
}
}
}
}

上例中的字段成员如果是一个数组或集合时,封装为属性时可能就相对麻烦,此时索引器的功能便派上用场,例:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Demo
{
 
class Program
{
static void Main(string[] args)
{
IndexerClass test = new IndexerClass();
 
//使用索引器赋值功能
test[0] = 24;
test[1] = 56;
test[2] = 55;
test[3] = 98;
 
for (int i = 0; i <= 3; i++)
{
//使用索引器读取功能
Console.WriteLine("元素{0}的值是{1}", i, test[i]);
}
 
Console.ReadLine();
}
}
 
class IndexerClass
{
//定义私有数组
private int[] arr = new int[100];
 
//定义公开索引器成员,用于访问 私有数组
public int this[int index]
{
//定义可以读
get
{
if (index < 0 || index >= 100)
return 0;
else
return arr[index];
}
//定义可写
set
{
if (!(index < 0 || index >= 100))
arr[index] = value;
}
}
}
}

另:索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。见下例:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
 
namespace Demo
{
 
class Program
{
static void Main(string[] args)
{
Product p = new Product();
Console.WriteLine("计算机的价格是:{0}", p["计算机"]);
 
Console.ReadLine();
}
}
 
class Product
{
//定义私有Hashtable存储产品名称与价格
private Hashtable products = new Hashtable();
 
//构造函数中初始化Hashtable数据
public Product()
{
products.Add("计算机", 6800.00m);
products.Add("手机", 2900.00m);
products.Add("PSP", 1500.00m);
}
 
//定义索引器使用字符型产品名
public decimal this[string key]
{
get
{
return Convert.ToDecimal(products[key]);
}
set
{
products[key] = value;
}
}
}
}
 
 
 
 
C#索引器示例
using System;
namespace ConsoleApplication2
{
public class Photo//这个类是一个相片类
{
string _title;//相片标题
public Photo(string title) {//初始化相片标题的构造方法
_title = title;
}
public string Title {//属性
get { return _title; }
}
}
public class Album
{//相册类
Photo[] photos; //用于存放照片的数组,
public Album(int capacity)
{//初始化相册大小
photos = new Photo[capacity];
}
///
/// 所引器,传递的索引用于对照片数组进行检索
///

///
///
public Photo this[int index]
{
get//get方法用于根据索引从索引器中获取照片
{
if (index < 0 || index >= photos.Length)//有效的索引
{
Console.WriteLine("索引无效");
return null;
}
else
{
return photos[index];
}
}
set
{
if (index < 0 || index >= photos.Length)
{
Console.WriteLine("索引无效");
return;//set 无返回 所以 return 就可以了
}
else
{
photos[index] = value;
}
}
}
///
/// 只读索引
///

///
///
public Photo this[string title]
{
get
{
foreach (Photo p in photos)
{
if (p.Title == title)//判断
return p;
}
Console.WriteLine("没有该照片");
return null;
}
}
}
class Test
{
static void Main(string[] arsg)
{
Album friends = new Album(3);//创建相册大小为3
//创建3张照片
Photo first = new Photo("逍遥");
Photo second = new Photo("太子");
Photo third = new Photo("姚佳");
friends[0] = first;
friends[1] = second;
friends[2] = third;
//一下为按照索引进行查询
Photo objPhoto = friends[2];
Console.WriteLine(objPhoto.Title);
//按名称进行检查
Photo obj2Photo = friends["太子"];
Console.WriteLine(obj2Photo.Title);
}
}
}
 
 
就是说,索引器对存在数组和集合的类,可以快速的赋值和读取!
 
阅读(1724) | 评论(0) | 转发(0) |
0

上一篇:c#属性详解

下一篇:C# 事件 委托

给主人留下些什么吧!~~