I come, I see, I conquer

                    —Gaius Julius Caesar

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
1. 索引器的作用是什么?

   C#通过提供索引器,可以象处理数组一样处理对象。特别是属性,每一个元素都以一个get或set方法暴露。最大的好处是使代码看上去更自然,更符合实际的思考模式。索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数,如以下示例2所示。

2. 索引器的使用难吗?

   索引指示器并不难使用。它们的用法跟数组相同。在一个类内部,你可以按照你的意愿来管理一组数据的集合。这些对象可以是类成员的有限集合,也可以是另外一个数组,或者是一些复杂的数据结构。不考虑类的内部实现,其数据可以通过使用索引指示器来获得。

3. 怎样定义一个索引器?

   语法:类型 this[类型 参数] { get;set;} 
   举例: 
public string this[int pos]
    
{
        
get
        
{
            
return strArray[pos];
        }

        
set
        
{
            strArray[pos] 
= value;
        }

    }

4. 使用索引器

示例1:


using System;

class Indexer
{
    
private string[] strArray;

    
public Indexer(int size)
    
{
        strArray 
= new string[size];
        
for (int i = 0; i < size; i++)
        
{
            strArray[i] 
= "empty";
        }

    }

    
    
public string this[int pos] //声明一个索引器,索引器不支持static,索引器只支持实例
    {
        
get
        
{
            
return strArray[pos]; //返回一个值
        }

        
set
        
{
            strArray[pos] 
= value; //value是一个属性值
        }

    }


    
static void Main(string[] args)
    
{
        Indexer indexer 
= new Indexer(10); //创建对象indexer
        indexer[9] = "Some Value"; //像对待数组一样对待对象indexer //使用索引器
        indexer[3= "Another Value";
        indexer[
5= "Any Value";
        Console.WriteLine(
"Indexer Output:");
        
for (int i = 0; i < 10; i++)
        
{
            Console.WriteLine(
"indexer[{0}]: {1}", i, indexer[i]); //使用索引器
        }

        Console.ReadKey();
    }

}

运行结果:
Indexer Output:
indexer[0]: empty
indexer[1]: empty
indexer[2]: empty
indexer[3]: Another Value
indexer[4]: empty
indexer[5]: Any Value
indexer[6]: empty
indexer[7]: empty
indexer[8]: empty
indexer[9]: Some Value 

示例2:

using System;

/// <summary>
/// 员工类
/// </summary>

public class Employee
{
    
private int age;
    
private string name;

    
public Employee(int age, string name)
    
{
        
this.age = age;
        
this.name = name;
    }


    
public string Name
    
{
        
get
        
{
            
return name;
        }

    }


    
public int Age
    
{
        
get
        
{
            
return age;
        }

    }


    
public override string ToString()
    
{
        
return "Name:" + name + ", Age:" + age;
    }

}

/// <summary>
/// 部门类
/// </summary>

class Department
{
    
private int i = 0;
    
private Employee[] employees;

    
public Department(params Employee[] emList)
    
{
        employees 
= new Employee[emList.Length];
        
foreach (Employee e in emList)
        
{
            employees[i
++= e;
        }

    }

    
/// <summary>
    
/// 创建索引器,通过string索引
    
/// </summary>
    
/// <param name="name"></param>
    
/// <returns></returns>

    public Employee this[string name]
    
{
        
get 
        
{
            
foreach (Employee e in employees)
            
{
                
if (e.Name == name)
                    
return e;
            }

            
return null;
        }

    }

    
/// <summary>
    
/// 创建索引器,通过int索引
    
/// </summary>
    
/// <param name="age"></param>
    
/// <returns></returns>

    public Employee this[int index]
    
{
        
get
        
{
            
if ((index < 0|| (index > employees.Length - 1))
            
{
                
return null;
            }

            
return employees[index];
        }

    }

}

/// <summary>
/// 索引器测试类
/// </summary>

class Application
{
    
static void Main(string[] args)
    
{
        Employee[] empList 
= new Employee[3];
        empList[
0= new Employee(20"Lee yang");
        empList[
1= new Employee(21"Liu yang");
        empList[
2= new Employee(22"Lan yang");

        Department dept 
= new Department(empList);

        Console.WriteLine(dept[
"Jim"]);
        Console.WriteLine(dept[
"Liu yang"]);
        Console.WriteLine(dept[
2]);

        Console.ReadKey();
    }

}

运行结果:

Name: Liu yang, Age: 21
Name: Lan yang, Age: 22

注意粗体部分的代码。
这样的话,我们就创建了索引器,我们就可以通过字符串或整型数索引来得到部门(Department)里对应的员工(Employee)了。 

                                                                                                                                         摘自互联网
posted on 2008-02-29 08:45  jcsu  阅读(1471)  评论(1)    收藏  举报